gpt4 book ai didi

python - 数据框中每行的最大值的列名

转载 作者:行者123 更新时间:2023-12-04 07:16:53 25 4
gpt4 key购买 nike

我有一个数据框,我想要一列填充每一行的最大值,所以我使用了这个:

 df_1['Highest_Rew_patch'] = df_1.max(axis=1)
输出:
    Patch_0  Patch_1  Patch_2  ...  Patch_7  exp_patch  Highest_Rew_patch
0 0.0 70.0 70.0 ... 0.0 3 70.0
1 0.0 74.0 74.0 ... 0.0 4 74.0
2 0.0 78.0 78.0 ... 0.0 4 78.0
3 0.0 82.0 82.0 ... 0.0 4 82.0
4 0.0 82.0 82.0 ... 0.0 5 82.0
5 0.0 86.0 86.0 ... 0.0 6 86.0
6 0.0 90.0 90.0 ... 0.0 6 90.0
7 0.0 94.0 94.0 ... 0.0 6 94.0
8 0.0 98.0 98.0 ... 0.0 6 98.0
9 0.0 98.0 98.0 ... 0.0 7 98.0
但是我想要一个有点不同的结果:
Patch_0  Patch_1  Patch_2   Patch_7  exp_patch  Highest_Rew_patch
0 0.0 70.0 70.0 3 Patch_2,Patch_7...
1 0.0 74.0 74.0 4 Patch_2,Patch_7...
因此,我想要具有最高值的特定行的该列的标题,而不是该行的最高值。

最佳答案

您可以选择以 Patch 开头的列然后只保留值等于最大值的列名:

>> s = df.iloc[:, df.columns.str.startswith('Patch')].apply(
lambda s: s.index[s.eq(s.max())].tolist(), axis=1)

>> s

0 [Patch_1, Patch_2]
1 [Patch_1, Patch_2]
2 [Patch_1, Patch_2]
3 [Patch_1, Patch_2]
4 [Patch_1, Patch_2]
5 [Patch_1, Patch_2]
6 [Patch_1, Patch_2]
7 [Patch_1, Patch_2]
8 [Patch_1, Patch_2]
9 [Patch_1, Patch_2]
或者作为字符串加入:
>> s = s.apply(lambda s: ','.join(s))

>> s
0 Patch_1,Patch_2
1 Patch_1,Patch_2
2 Patch_1,Patch_2
3 Patch_1,Patch_2
4 Patch_1,Patch_2
5 Patch_1,Patch_2
6 Patch_1,Patch_2
7 Patch_1,Patch_2
8 Patch_1,Patch_2
9 Patch_1,Patch_2
dtype: object
然后只需分配新列:
df['Highest_Rew_patch'] = s

关于python - 数据框中每行的最大值的列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68713249/

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