简体   繁体   中英

How do I eliminate the additional row for aggregate column in pandas groupby

I am trying to do some aggregation of fields and I get an additional row on the aggregate column- in this case 'sum'. How do I get rid of this? This makes it difficult to process this dataframe in subsequent processing

import pandas as pd
Grade=[['A',1],['A',2],['A',10],['B',4],['B',2],['C',3],['D',10],['D',5],['D',1]]
Grade_df=pd.DataFrame(Grade,columns=['grade','count'])    
Grade_Aggregate=Grade_df.groupby(['grade']).agg({'count':['sum']}).reset_index() 

Grade_Aggregate

  grade count
          sum
0     A    13
1     B     6
2     C     3
3     D    16

If you want to remove the 'Sum':

Grade_Aggregate.columns=[i[0] for i in Grade_Aggregate.columns]

If you want it to be appended to the column name:

Grade_Aggregate.columns=["_".join(i) for i in Grade_Aggregate.columns]

You can use Groupby.sum here directly.

Grade_df.groupby('grade', as_index=False)['count'].sum()

  grade  count
0     A     13
1     B      6
2     C      3
3     D     16

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM