dreevo
dreevo

Reputation: 29

Getting the max value of a DataFrame

I have a dataframe with indexes names of countries and columns medals. I want to get the name of the country with the most number of gold medals. I've tried this :

def answer_one():
    x= df[df['Gold.2']==df['Gold.2'].max()]
    return x.index
answer_one()

I want to get just the string which is the name of the country but instead I keep getting this

Index(['United States'], dtype='object')

Upvotes: 0

Views: 141

Answers (2)

Biranchi
Biranchi

Reputation: 16327

def answer_one():
    x= df[df['Gold.2']==df['Gold.2'].max()]
    return x.index.values[0]
answer_one()

Upvotes: 1

Code Pope
Code Pope

Reputation: 5459

As you want the concrete value, I would use the following code:

def answer_one():
    x= df[df['Gold.2']==df['Gold.2'].max()]
    return x.index.values[0]
answer_one()

This will return the first max country. If you want an array of all max countries:

def answer_one():
    x= df[df['Gold.2']==df['Gold.2'].max()]
    return x.index.values
answer_one()

Upvotes: 1

Related Questions