问题描述:

Write a function that creates a Series called "Points" which is a weighted value where each gold medal (Gold.2) counts for 3 points, silver medals (Silver.2) for 2 points, and bronze medals (Bronze.2) for 1 point. The function should return only the column (a Series object) which you created.

This function should return a Series named Points of length 146

I'm new to python and pandas and I don't know if I'm on the right track or not with this code but I'm getting a key error

KeyError: "['Gold.2' 'Silver.2' 'Bronze.2'] not in index" for this code:

import pandas as pd

df = pd.read_csv('olympics.csv', index_col=0, skiprows=1)

def answer_four():

df['Points'] = df[(df[['Gold.2','Silver.2','Bronze.2']], [3,2,1])]

#df[['Gold.2','Silver.2','Bronze.2']].apply(lambda x:(x,[3,2,1]))

olympic_points_df = df[['Points']]

return olympic_points_df

answer_four()

Any help with this would be much appreciated.

网友答案:

Some similar to this would do the trick

def answer_four():
    df['Points'] = df['Gold.2']*3 + df['Silver.2']*2 + df['Bronze.2'] 
    return df['Points']
相关阅读:
Top