Group dataframe and get sum AND count?

前端 未结 4 1965
南方客
南方客 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

    If you have lots of columns and only one is different you could do:

    In[1]: grouper = df.groupby('Company Name')
    In[2]: res = grouper.count()
    In[3]: res['Amount'] = grouper.Amount.sum()
    In[4]: res
    Out[4]:
                          Organisation Name   Amount
    Company Name                                   
    Vifor Pharma UK Ltd                  5  4207.93
    

    Note you can then rename the Organisation Name column as you wish.

提交回复
热议问题