A CSV file is given. I am supposed to print the name of the row label of a row of the data frame as a string output. How do I do that?
import pandas as pd
df= pd.read_csv('olympics.csv', index_col=0, skiprows=1)
s= df.loc[df['Gold'].idxmax()]
return s.index
Here 'Gold' is a random column index name. I have been trying by this code. But it only prints column indices. But I need to print the row index output as a string.
df = pd.DataFrame({'id':['1','2','3','4','5','6','7','8'],
'A':['foo','bar','foo','bar','foo','bar','foo','foo'],
'C':[10, 10, 10, 30, 50, 60, 50, 8],
'D':[9, 8, 7, 6, 5, 4, 3, 2]},
index = list('abcdefgh'))
idxmax()
returns the row index,
>>> df['C'].idxmax()
'f'
Selecting that row produces a Series whose name is the index of that row.
>>> df.loc[df['C'].idxmax()]
id 6
A bar
C 60
D 4
Name: f, dtype: object
>>> df.loc[df['C'].idxmax()].name
'f'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.