[英]Retrieving the value of the column with the maximum values
I have a panda's dataframe. 我有一只熊猫的数据框。
It looks like this: 它看起来像这样:
level_0 level_1 from to
0 0 0 0.927273 0.300000
1 1 1 0.946667 0.727273
2 1 2 0.565657 0.200000
3 1 3 0.946667 0.083333
4 2 4 0.831818 1.000000
5 3 5 0.831818 0.818182
6 4 6 0.872727 0.666667
7 5 7 1.000000 0.700000
8 6 8 1.000000 1.000000
9 7 9 1.000000 0.666667
I want to output the (level_0, level_1) pairs that have the highest combined from + to scores. 我想输出从+到分数组合最高的(level_0,level_1)对。 These are obvious for most of them, but in the case of level_0 = 1, I have three possibilities. 对于大多数人来说这些是显而易见的,但是在level_0 = 1的情况下,我有三种可能性。 I want the algorithm to output (1,1) because they have the highest combined from + to scores. 我希望算法输出(1,1)因为它们具有从+到分数的最高组合。
How do I achieve this? 我该如何实现这一目标?
Thanks in advance and my excuses for the reckless initial question. 提前谢谢,并为我鲁莽的初步问题找借口。
Do you want: 你想要:
# this runs on the original double-indexed dataframe
df[['from','to']].sum(1).groupby(level=0).idxmax()
Output: 输出:
level_0
0 (0, 0)
1 (1, 1)
2 (2, 4)
3 (3, 5)
4 (4, 6)
5 (5, 7)
6 (6, 8)
7 (7, 9)
dtype: object
You can use this: 你可以用这个:
df.set_index(['level_0','level_1'])\
.assign(total_score = (df['from']+df['to']).to_numpy())['total_score']\
.groupby(level=0).idxmax()
Output: 输出:
level_0
0 (0, 0)
1 (1, 1)
2 (2, 4)
3 (3, 5)
4 (4, 6)
5 (5, 7)
6 (6, 8)
7 (7, 9)
Name: total_score, dtype: object
The pandas way is to compute the sum of the columns, and search where that sum is equal to its maximum value. 大熊猫的方法是计算列的总和,并搜索该和等于其最大值的位置。
I would use: 我会用:
score = df['to'] + df['from']
print(df[score == score.max()])
With the current example, it gives : 在目前的例子中,它给出了:
level_0 level_1 from to
8 6 8 1.000000 1.000000
If the dataframe was multi_indexed like dfi = df.set_index(['level_0', 'level_1'])
, it is exactly the same: 如果数据帧是multi_indexed,如dfi = df.set_index(['level_0', 'level_1'])
,则它完全相同:
scorei = dfi['from'] + dfi['to']
print(dfi[scorei == scorei.max()])
which gives: 这使:
from to
level_0 level_1
6 8 1.0 1.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.