Pandas groupby stored in a new dataframe

前端 未结 2 461
暖寄归人
暖寄归人 2021-01-22 18:44

I have the following code:

import pandas as pd
df1 = pd.DataFrame({\'Counterparty\':[\'Bank\',\'Bank\',\'GSE\',\'PSE\'],
            \'Sub Cat\':[\'Tier1\',\'Sma         


        
相关标签:
2条回答
  • 2021-01-22 19:07

    For columns from index add as_index=False parameter or reset_index:

    df2=df1.groupby(['Counterparty','Location'])[['Amount']].sum().reset_index()
    print (df2)
      Counterparty Location  Amount
    0         Bank       US     105
    1          GSE       UK      65
    2          PSE       UK      55
    
    df2=df1.groupby(['Counterparty','Location'], as_index=False)[['Amount']].sum()
    print (df2)
      Counterparty Location  Amount
    0         Bank       US     105
    1          GSE       UK      65
    2          PSE       UK      55
    

    If aggregate by all columns here happens automatic exclusion of nuisance columns - column Sub Cat is omitted:

    df2=df1.groupby(['Counterparty','Location']).sum().reset_index()
    print (df2)
      Counterparty Location  Amount  Amount1
    0         Bank       US     105        3
    1          GSE       UK      65        3
    2          PSE       UK      55        4
    
    
    df2=df1.groupby(['Counterparty','Location'], as_index=False).sum()
    
    0 讨论(0)
  • 2021-01-22 19:09

    Remove the double brackets around the 'Amount' and make them single brackets. You're telling it to only select one column.

    0 讨论(0)
提交回复
热议问题