熊猫的最大和总和

时间:2020-06-24 17:35:04

标签: python pandas count max

我有以下数据集:

import pandas as pd
df = pd.DataFrame({'In':['A','B','D','Z','Q','E'],
                   'Out' : ['Z', 'D', 'F', 'H', 'Z', 'A'],
                   'Score_in' : ['6', '2', '1', '0', '1', '3'], 
                   'Score_out' : ['2','3','0', '1','1','3'],
                   'Place' : ['One','Two','Four', 'Two','Two','One']})

我想得到两个输出:

  1. 每个地方有多少个得分点?
  2. 哪个是得分最高(总和)的“地方”?

让我们假设我有不止一个这样的df,或者相同的,是“ year”一栏,我在其中分组以筛选想要的冠军。如何在x标签上有位置,年份和y标签上有得分点的位置上绘制每年所需的输出?

2 个答案:

答案 0 :(得分:0)

要求总分,可以使用groupby:

>>> df.groupby('Place')['Score_in', 'Score_out'].sum()
       Score_in  Score_out
Place                     
Four          1          0
One           9          5
Two           3          5

总金额:

>>> temp = df.groupby('Place')['Score_in', 'Score_out'].sum().sum(axis = 1)
>>> temp
Place
Four     1
One     14
Two      8
dtype: int64

要获得最大收益:

>>> temp[temp == temp.max()]
Place
One    14
dtype: int64

答案 1 :(得分:0)

这是第一个问题的答案:

df["Score_out"] = pd.to_numeric(df["Score_out"])
df["Score_in"] = pd.to_numeric(df["Score_in"])
res = df.assign(total = df.Score_in + df.Score_out).groupby("Place").sum()
print(res) 

#output: 

       Score_in  Score_out  total
Place                            
Four          1          0      1
One           9          5     14
Two           3          5      8

res.sort_values("total", ascending = False).index[0]

输出:

'One'