Pandas - Return A Dataframe After Groupby
I have a Pandas df: Name No A 1 A 2 B 2 B 2 B 3 I want to group by column Name, sum column No and then return a 2-column dataframe like this: Name
Solution 1:
Add parameter as_index=False
to groupby
:
print (df.groupby(['Name'], as_index=False)['No'].sum())
Name No
0 A 31 B 7
Or call reset_index
:
print (df.groupby(['Name'])['No'].sum().reset_index())
Name No
0A31B7
Post a Comment for "Pandas - Return A Dataframe After Groupby"