Can someone help me understand why I am having issues with using "groupby and sum" in Pandas. I have data where col_1
is a string and col_2
is a column of ones I am using to create additional variables by group. I have the following for calculating a cumulative sum within groups that appears to work as expected:
df['var'] = df.groupby(['col_1'])['col_2'].cumsum()
(this is working fine to my knowledge)
However, when I attempt to calculate a sum or max by group, the resulting column is all nan
and I am struggling to understand how or why this is happening to me.
df['var'] = df.groupby(['col_1'])['col_2'].cumsum()
(this is creating a column of nan
)
Appreciate the help here - thanks!
I was trying to calcuate a sum by group (which should be 1 or 2) but instead am receiving just nan
values.