Im trying to use groupby function, is there a way to use a specific element rather than the column name. Example of code.
df.groupby(['Month', 'Place'])['Number'].sum()
This is what I want to do.
df.groupby(['April', Place'])['Number'].sum()
CodePudding user response:
you need filter your DataFrame before:
df.loc[df['Month'].eq('April')].groupby('Place')['Number'].sum()
#df[df['Month'].eq('April')].groupby('Place')['Number'].sum()
CodePudding user response:
Yes, you can pass as many columns to groupby
df = pd.DataFrame([['a', 'x', 1], ['a', 'x', 2], ['b', 'y',3], ['b', 'z',4]])
df.columns = ['c1', 'c2', 'c3']
df.groupby(['c1', 'c2'])['c3'].mean()
resuls in
c1 c2
a x 1.5
b y 3.0
z 4.0