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
名称:kills,dtype:int64 kills,而有记录以来最多的杀死是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/

10-12 21:17