简体   繁体   English

获取 DataFrame 的最大值

[英]Getting the max value of a DataFrame

I have a dataframe with indexes names of countries and columns medals.我有一个 dataframe 索引国家名称和列奖牌。 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')
def answer_one():
    x= df[df['Gold.2']==df['Gold.2'].max()]
    return x.index.values[0]
answer_one()

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM