Get Index Minimum Value in Column When String - Pandas Dataframe

PYB :

I have done some research on this, but couldn't find a concise method when the index is of type 'string'.

Given the following Pandas dataframe:

Platform | Action |    RPG    | Fighting
----------------------------------------
PC       |   4    |      6    |     9
Playstat |   6    |      7    |     5
Xbox     |   9    |      4    |     6
Wii      |   8    |      8    |     7

I was trying to get the index (Platform) of the smallest value in the 'RPG' column, which would return 'Xbox'. I managed to make it work but it's not efficient, and looking for a better/quicker/condensed approach. Here is what I got:

# 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]
ansev :

Use Series.idxmin:

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

or what @Quang Hoang suggests on the comments

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

if Platform already the index:

df['RPG'].idxmin()

EDIT

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

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

if already the index:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=11381&siteId=1