Group dataframe and get sum AND count?

前端 未结 4 1973
南方客
南方客 2020-11-30 22:04

I have a dataframe that looks like this:

              Company Name              Organisation Name  Amount
10118  Vifor Pharma UK Ltd  Welsh Assoc for Gastro         


        
4条回答
  •  孤街浪徒
    2020-11-30 22:27

    Just in case you were wondering how to rename columns during aggregation, here's how for

    pandas >= 0.25: Named Aggregation

    df.groupby('Company Name')['Amount'].agg(MySum='sum', MyCount='count')
    

    Or,

    df.groupby('Company Name').agg(MySum=('Amount', 'sum'), MyCount=('Amount', 'count'))
    

                           MySum  MyCount
    Company Name                       
    Vifor Pharma UK Ltd  4207.93        5
    

提交回复
热议问题