如何在数据透视表中查找列的位置

时间:2019-11-04 18:33:59

标签: python pandas pivot pivot-table

嗨,我有一个使用ivot_table()方法的数据透视表。

我想找出该行中值最高的列。我不确定如何遍历数据透视

此pivot_df

EventID 1.0 2.0 3.0 4.0 5.0 6.0
Name                        
John    10  90  0   70  30  50
Berry   20  50  30  0   0   0
Charles 50  20  0   80  40  60
Susan   60  30  30  0   30  0
Elisa   200 30  30  100 0   0

预期输出:

Give me the eventID for highest amount of Charles = 4.0
Give me the eventID for highest amount of John = 2.0

我尝试了.loc方法,但前提是我知道EventID。我只有名字和金额

pivot_df.loc(Berry,3.0)  = 30

1 个答案:

答案 0 :(得分:1)

它是.locidxmax

df.loc['Charles'].idxmax()

Out[151]: 4.0

df.loc['John'].idxmax()

Out[150]: 2.0

如果您想同时获得两者

df.idxmax(1).loc[['Charles', 'John']]

Out[153]:
Charles    4.0
John       2.0
dtype: float64
相关问题