gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:59:28 24 4
gpt4 key购买 nike

嗨,我有一个使用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

最佳答案

它是.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

关于python - 如何查找数据透视表中列的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58699218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com