在字符串为Dataframe时获取列中的索引最小值

2024-04-16 03:31:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我对此做了一些研究,但当索引类型为“string”时,找不到一个简洁的方法

给定以下数据帧:

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]

Tags: ofthe方法dataindexvalueresultmin
1条回答
网友
1楼 · 发布于 2024-04-16 03:31:52

使用^{}

df.set_index('Platform')['RPG'].idxmin()
#'Xbox'

或者@Quang Hoang在评论中提出了什么建议

df.loc[df['RPG'].idxmin(), 'Platform']

如果Platform已经存在索引:

df['RPG'].idxmin()

编辑

df.set_index('Platform').loc['Playstat'].idxmin()
#'Fighting'

df.set_index('Platform').idxmin(axis=1)['Playstat']
#'Fighting'

如果索引中已包含:

df.loc['Playstat'].idxmin()

相关问题 更多 >