【发布时间】:2020-06-04 00:39:13
【问题描述】:
我对此进行了一些研究,但是当索引为'string'类型时找不到简洁的方法。
给定以下 Pandas 数据框:
Platform | Action | RPG | Fighting
----------------------------------------
PC | 4 | 6 | 9
Playstat | 6 | 7 | 5
Xbox | 9 | 4 | 6
Wii | 8 | 8 | 7
我试图获取“RPG”列中最小值的索引(平台),这将返回“Xbox”。我设法让它工作,但它效率不高,并且正在寻找更好/更快/浓缩的方法。这是我得到的:
# Return the minimum value of a series of all columns values for RPG
series1 = min(ign_data.loc['RPG'])
# Find the lowest value in the series
minim = min(ign_data.loc['RPG'])
# Get the index of that value using boolean indexing
result = series1[series1 == minim].index
# Format that index to a list, and return the first (and only) element
str_result = result.format()[0]
【问题讨论】:
-
ign_data['RPG'].idxmin() 有效吗?
-
确实如此,先生 :)
标签: python pandas dataframe indexing series