I am writing a program using data from the Olympics.
The question being asked is:
Which country has won the most gold medals in summer games?
The answer is:
the USA
These are some of the variables and data points for reference:
# Summer Gold Silver Bronze Total medals
United Arab Emirates 8 1 0 0 1
United States 26 976 757 666 2399
Uruguay 20 2 2 6 10
The function below should return a single string value but instead it is returning a pandas Data Frame
def answer_one():
return df.loc[df['Gold'] == df['Gold'].max()]
How can I correct this function?
Do this instead:
def answer_one():
return list(df.loc[df['Gold'] == df['Gold'].max()].index)
That should give you a list of countries with max golds if there are ties. If there are no ties, it will just give a list containing one entry: in your case, the result will be ['US']
.
如果 Country name 是您的索引列,这应该可以解决问题:
df.loc[df['Gold'].idxmax()]
you can try this. It worked to me
df.Gold.idxmax()
Cheers
def answer_one():
return df[df['Gold'] == df['Gold'].max()].index[0]
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.