Python 熊猫的最大值和总和

Python 熊猫的最大值和总和,python,pandas,count,max,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',

我有以下数据集:

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']})
我希望得到两个输出:

  • 每个地方有多少得分
  • 哪一个“地方”得分最多(总和)

  • 让我们假设我有不止一个像这样的df,或者,这是相同的,一个“年”列,在这里我分组筛选我想要的冠军。如果x标签上有位置、年份,y标签上有分数,我如何绘制每年所需的输出?

    对于总分,您可以使用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
    

    以下是第一个问题的答案:

    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'
    

    内特,你问了一大堆问题,我会把它们分成不同的帖子/问题。关于groupby的第一个问题,请你提供所需的输出,好吗?你说得对,罗伊,我道歉。这里有一个相关的问题,如果你能帮助我:当然。我来看看。