gpt4 book ai didi

python - 如何根据pandas数据框中其他列的应用条件提取列值

转载 作者:行者123 更新时间:2023-12-01 08:53:13 25 4
gpt4 key购买 nike

print("Someone has killed {:.4f} with headshot, have {} kills, while the most kills ever recorded is {}.".format(pubg_main_df['headshotKills'].max(), pubg_main_df[pubg_main_df['headshotKills']==pubg_main_df['headshotKills'].max()]['kills'], pubg_main_df['kills'].max()))

我想要的是在 headshotKills 最大的情况下获取“Kills”的值。

但我得到:

有人爆头杀死了 26.0000 人,有 910050 60 人名称:杀戮,数据类型:int64 杀戮,而记录的最多杀戮是 60。

应该是:

有人爆头杀死了 26.0000 人,共击杀 42 人,而历史记录最多击杀人数为 60 人。

变量的类型是:

headshotKills int64

杀死 int64

请帮忙。 :)

最佳答案

使用set_indexidxmax :

#sample data
pubg_main_df= pd.DataFrame({'kills':[60, 42], 'headshotKills':[3, 26]})

print("Someone has killed {:.4f} with headshot, have {} kills, while the most kills ever recorded is {}."
.format(pubg_main_df['headshotKills'].max(),
pubg_main_df.set_index('kills')['headshotKills'].idxmax(),
pubg_main_df['kills'].max()))


Someone has killed 26.0000 with headshot, have 42 kills, while the most kills ever recorded is 60.

关于python - 如何根据pandas数据框中其他列的应用条件提取列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52968589/

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