【问题标题】:how to find location of column in pivot table如何在数据透视表中查找列的位置
【发布时间】:2019-11-04 18:33:59
【问题描述】:

您好,我有一个使用 pivot_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

【问题讨论】:

    标签: python pandas pivot pivot-table


    【解决方案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
    

    【讨论】:

    • @user11666514:不客气。很高兴我能帮忙:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 2015-11-01
    相关资源
    最近更新 更多