【问题标题】:How to get index and column of max value? [duplicate]如何获取最大值的索引和列? [复制]
【发布时间】:2019-09-30 16:13:01
【问题描述】:

我想获得最大总数的索引和值(非零)。

A = np.matrix([[0,2,1,0],
               [0,0,0,0],
               [1,3,0,1],])
slst = ['S1','S2','S3','S4']
namelist= ['Alice','John','Joe']
df =  pd.DataFrame(A,columns = slst, index = namelist)
df.loc['total'] = df.select_dtypes(pd.np.number).sum()
print (df)
Output:
       S1  S2  S3  S4
Alice   0   2   1   0
John    0   0   0   0
Joe     1   3   0   1
total   1   5   1   1

我想从下表中得到结果。 如何得到它?

Output1:
Alice: 2,
Joe: 3

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    这也应该有效:

    df1 = df.max(axis=1)
    df1
    

           S1  S2  S3  S4
    Alice   0   2   1   0
    John    0   0   0   0
    Joe     1   3   0   1
    total   1   5   1   1
    
    Alice    2
    John     0
    Joe      3
    total    5
    dtype: int64
    

    【讨论】:

      【解决方案2】:

      不要df.loc['total'],试试:

      total = df.select_dtypes(pd.np.number).sum()
      df[total.idxmax()]
      

      输出:

      Alice    2
      John     0
      Joe      3
      total    5
      Name: S2, dtype: int64
      

      【讨论】:

        猜你喜欢
        • 2016-08-04
        • 1970-01-01
        • 2018-04-25
        • 2018-06-09
        • 2019-01-20
        • 2014-07-09
        • 1970-01-01
        • 2019-05-09
        • 1970-01-01
        相关资源
        最近更新 更多