In the following code it shows a grouping, adding the total of occurrences, assuming that you only have df2
, what is the best way to obtain df
?
>>> import pandas as pd
>>> d = {
"var_1":['a','a','a','c','c','c','c'],
"var_2":['b','b','b','d','d','d','d'],
"total":[1,1,1,1,1,1,1],
"days":['4','4','4','2','2','2','2']
}
>>> # Create a dataframe since a dictionary
>>> df = pd.DataFrame(d)
>>> df
var_1 var_2 total days
0 a b 1 4
1 a b 1 4
2 a b 1 4
3 c d 1 2
4 c d 1 2
5 c d 1 2
6 c d 1 2
>>> df2 = df.groupby(['var_1','var_2','days']).sum('total').reset_index()
>>> df2
var_1 var_2 days total
0 a b 4 3
1 c d 2 4