def q05_difference_in_gold_medal(path): "write your solution here" df = q02_rename_columns(path) df = df[:-1] #golds= df.iloc[:,[2,7]] gold_diff = df.iloc[:, 2].astype(int) - df.iloc[:, 7].astype(int) return gold_diff.max()
def q03_summer_gold_medals(path): df = q02_rename_columns(path) df['country name'], df['country_code'] = df['country name'].str.split( '(', 1).str df.set_index('country name', inplace=True) df.drop(labels=['Combined total'], axis=1, inplace=True) return df[0:146]
def q06_get_points(path): df = q02_rename_columns(path) df1 = df.iloc[:, [2, 7, 12]].astype(int).sum(axis=1) * [3] df2 = df.iloc[:, [3, 8, 13]].astype(int).sum(axis=1) * [2] df3 = df.iloc[:, [4, 9, 14]].astype(int).sum(axis=1) df['Points'] = ((df1 + df2 + df3) / 2).astype(int) return df['Points']
def q03_summer_gold_medals(path): df = q02_rename_columns(path) df['country name'], df['country code'] = df['country name'].str.split( '(', 1).str df.set_index('country name', inplace=True) del df['Combined total'] return df[1:147]
def q06_get_points(path): df = q02_rename_columns(path) df = df.iloc[:, :-1] df_silver = (df['Silver']).astype(int) df_gold = (df['Gold']).astype(int) df_bronze = (df['Bronze']).astype(int) df['To'] = ((np.sum(df_gold * 3, axis=1) / 2) + (np.sum(df_silver * 2, axis=1)/2) + (np.sum(df_bronze * 1, axis = 1)) /2) return (df['To'].astype(int))
def q06_get_points(path): 'write your solution here' df = q02_rename_columns(path) df = df[:-1] df['Points'] = np.sum(df['Gold'].astype(int) * 3, axis=1) + np.sum( df['Silver'].astype(int) * 2, axis=1) + np.sum( df['Bronze'].astype(int), axis=1) return df['Points']
def q06_get_points(path): df = q02_rename_columns(path) gold_points = df['Gold'].iloc[:-1,2].astype(int)*3 silver_points = df['Silver'].iloc[:-1,2].astype(int)*2 bronze_points = df['Bronze'].iloc[:-1,2].astype(int)*1 total_points = gold_points + silver_points + bronze_points df['Points'] = total_points return total_points
def q05_difference_in_gold_medal(path): 'write your solution here' df = q02_rename_columns(path) df=df[:-1] #Drop last row which is total of all individual columns df['diff_max'] = pd.DataFrame(df.iloc[:,2].astype('int64')) - pd.DataFrame(df.iloc[:,7].astype('int64')) return df['diff_max'].max()
def q03_summer_gold_medals(path): 'write your solution here' df = q02_rename_columns(path) df['country name'] = df['country name'].str.replace('(', '-').str.replace( ')', '').str.replace('[', '').str.replace(']', '').str.split('-').str[0] df.set_index('country name', inplace=True) df.drop('Totals', axis=0, inplace=True) return df
def q03_summer_gold_medals(path): "write your solution here" df = q02_rename_columns(path) df['country name'] = df['country name'].apply(lambda x: x.split('(')[0]) new_index = df['country name'] df.drop(['country name'], axis=1, inplace=True) df.index = new_index df.drop(labels='Totals', axis=0, inplace=True) return df
def q06_get_points(path): df = q02_rename_columns(path) df.iloc[:, 12:15] = df.iloc[:, 12:15].apply(pd.to_numeric) df['Points1'] = (df.iloc[:, 12:13] * 3) df['Points2'] = (df.iloc[:, 13:14] * 2) df['Points3'] = (df.iloc[:, 14:15] * 1) df['Points'] = df['Points1'] + df['Points2'] + df['Points3'] df['Points'] = df['Points'] * 2 return (pd.Series(df['Points']))
def q03_summer_gold_medals(path): "write your solution here" df = q02_rename_columns(path) country_cols = pd.DataFrame(df['country name'].str.split('(', 1).tolist(), columns=['country name', 'country code']) df = df.drop(['country name'], axis=1) df.loc[:, 'country name'] = country_cols['country name'] df = df.set_index(keys=['country name']) return df[:-1]
def q03_summer_gold_medals(path): "write your solution here" df = q02_rename_columns(path) df_1 = pd.DataFrame(df['country name'].str.split('(').tolist(), columns = ['country name','country code']) df = df.drop(['country name'], axis=1) df = pd.concat([pd.DataFrame(df_1['country name']), df], axis=1) df = df.set_index('country name') df =df.drop(['Totals'], axis=0) return df[:-1]
def q05_difference_in_gold_medal(path): df = q02_rename_columns(path) dfnew = pd.DataFrame() dfnew['Summer_gold'] = df.iloc[:, 2:3].astype(int) dfnew['Winter_gold'] = df.iloc[:, 7:8].astype(int) dfnew['country'] = df['country name'] dfnew['diff'] = abs(dfnew['Summer_gold'] - dfnew['Winter_gold']) country_diff = (dfnew.loc[dfnew['diff'].idxmax()])[3] #country_diff.replace("\xc2\xa0", " ") return country_diff
def q06_get_points(path): "write your solution here" df = q02_rename_columns(path) df['Gold'] = df['Gold'].astype(int) df['Silver'] = df['Silver'].astype(int) df['Bronze'] = df['Bronze'].astype(int) df['points'] = np.sum(df['Gold'] * 3, axis=1) + np.sum( df['Silver'] * 2, axis=1) + np.sum(df['Bronze'] * 1, axis=1) return df['points']
def q06_get_points(path): "write your solution here" df = q02_rename_columns(path="./data/olympics.csv") pt_g = df['Gold'].astype(int) * 3 pt_s = df['Silver'].astype(int) * 2 pt_b = df['Bronze'].astype(int) * 1 gold_pts = pt_g.iloc[:, 0] + pt_g.iloc[:, 1] + pt_g.iloc[:, 2] silver_pts = pt_s.iloc[:, 0] + pt_s.iloc[:, 1] + pt_s.iloc[:, 2] bronze_pts = pt_b.iloc[:, 0] + pt_b.iloc[:, 1] + pt_b.iloc[:, 2] Points = gold_pts + silver_pts + bronze_pts return Points
def q05_difference_in_gold_medal(path): df = q02_rename_columns(path) #print pd.DataFrame(df.iloc[:,2]) - pd.DataFrame(df.iloc[:,7]) cols = list(df.columns) cols[2] = 'SGold' cols[7] = 'WGold' cols[12] = 'TGold' df.columns = cols df['diff'] = pd.to_numeric(df['SGold']) - pd.to_numeric(df['WGold']) df = df.drop(147) return max(df['diff'])
def q03_summer_gold_medals(path): df = q02_rename_columns(path="./data/olympics.csv") c = [] "write your solution here" for i in df['country name']: n_co = i.split('(')[0].rstrip() c.append(n_co) df['country name'] = c ndf = df.set_index('country name') fdf = ndf.drop('Totals') return fdf
def q03_summer_gold_medals(path): df = q02_rename_columns(path) country = [] for i in df['country name']: i = i.rsplit('(')[0] i = i.replace("\xc2\xa0", "") country.append(i) df['country name'] = country df = df.set_index('country name') df = df.drop(['Totals']) return df
def q05_difference_in_gold_medal(path): df = q02_rename_columns(path) df1 = df.loc[:, ['country name', 'Gold']] df2 = df1.iloc[:, [0, 1, 2]] df3 = df2.set_index(keys='country name') df3.loc[:, :] = df3.loc[:, :].apply(pd.to_numeric) df3.drop(['Totals'], inplace=True) df4 = df3.iloc[:, 0] - df3.iloc[:, 1] df4 = df4.abs() return df4.max()
def q06_get_points(path): df = q02_rename_columns(path) df1 = df.loc[:, ['country name', 'Gold', 'Silver', 'Bronze']] df2 = df1.iloc[:, [0, 3, 6, 9]] df3 = df2 df3 = df3.loc[:, ['Gold', 'Silver', 'Bronze']].apply(pd.to_numeric) df3['Points'] = df3.loc[:, 'Gold'] * 3 + df3.loc[:, 'Silver'] * 2 + df3.loc[:, 'Bronze'] * 1 df3.drop(labels=['Gold', 'Silver', 'Bronze'], axis=1, inplace=True) return df3['Points']
def q05_difference_in_gold_medal(path): 'write your solution here' df = q02_rename_columns(path) df = df[:-1] df1 = df.rename(columns={ '01 !': 'Gold', '02 !': 'Silver', '03 !': 'Bronze' }) df_Sumer = pd.to_numeric(df1.iloc[:, 2]) df_win = pd.to_numeric(df1.iloc[:, 7]) df_1 = (df_Sumer) - (df_win) return (df_1).max()
def q03_summer_gold_medals(path): 'write your solution here' df = q02_rename_columns(path) cname = df.loc[:, 'country name'] for i in range(1, cname.shape[0]): cname[i] = cname[i].split('(')[0].rstrip( ) #Split the characters with '(' and strip the unnecessary characters using rstrip() df1 = df.drop(labels='country name', axis=1) df1.index = cname df2 = df1.drop(labels='Totals', axis=0) return df2
def q05_difference_in_gold_medal(path): df = q02_rename_columns(path) df_gold_medals = df.iloc[:, [0, 2, 7]] df_gold_medals_new = df_gold_medals.iloc[:, [1, 2]].astype(int) df_gold_medals.drop(labels=['Gold'], axis=1, inplace=True) df_gold_medals_new = pd.concat([df_gold_medals_new, df_gold_medals], axis=1) df_gold_medals_new[ 'Difference'] = df_gold_medals_new.iloc[:, 0] - df_gold_medals_new.iloc[:, 1] df_gold_final = df_gold_medals_new.iloc[:, [2, 3]][0:146] return df_gold_final[df_gold_final['Difference'] == df_gold_final['Difference'].max()].iloc[0, 1]
def q03_summer_gold_medals(path): 'write your solution here' df = q02_rename_columns(path) a = df['country name'] b = a.str.split('(', expand=True) c = b[0] # d = b[1] # e = d.str.split(')', expand=True) # f = list(e[0]) df = df.set_index(c) # df['country code'] = f df.drop(['Totals'], axis=0, inplace=True) df.drop(['country name'], axis=1, inplace=True) df.index.name = 'country name' return df
def q05_difference_in_gold_medal(path): l1 = [] l2 = [] df = q02_rename_columns(path) summer = df.iloc[:, 2] winter = df.iloc[:, 7] for i in summer: l1.append(int(i)) for j in winter: l2.append(int(j)) big_dif = [abs(x - y) for x, y in zip(l1, l2)] df['Summer - Winter'] = big_dif df.drop(axis=0, labels=[147], inplace=True) diff = df['Summer - Winter'].max() return diff
def q03_summer_gold_medals(path): "write your solution here" df = q02_rename_columns(path) #To split the country name into name and code based on '(' new_columns = pd.DataFrame(df['country name'].str.split('(', 1).tolist(), columns=['country_name', 'country_code']) #drop existing country name column df = df.drop(['country name'], axis=1) #Add new column country name based on above split step df.loc[:, 'country name'] = new_columns['country_name'] #set the newly added country name column as index. df = df.set_index(keys=['country name']) return df[:-1]
def q06_get_points(path): df = q02_rename_columns(path) cols = list(df.columns) cols[2] = 'SGold' cols[3] = 'SSilver' cols[4] = 'SBronze' cols[7] = 'WGold' cols[8] = 'WSilver' cols[9] = 'WBronze' cols[12] = 'TGold' cols[13] = 'TSilver' cols[14] = 'TBronze' df.columns = cols df1 = pd.DataFrame() df['Points'] = ((pd.to_numeric(df['TGold']) * 3) + (pd.to_numeric(df['TSilver']) * 2) + (pd.to_numeric(df['TBronze']))) * 2 return df['Points']
def q06_get_points(path): df = q02_rename_columns(path) df_gsb = df.iloc[:, [2, 3, 4, 7, 8, 9, 12, 13, 14]] df_gsb = df_gsb.astype('int') df_gsb['Total_Gold'] = df_gsb.iloc[:, 0] + df_gsb.iloc[:, 3] + df_gsb.iloc[:, 6] df_gsb['Total_Silver'] = df_gsb.iloc[:, 1] + df_gsb.iloc[:, 4] + df_gsb.iloc[:, 7] df_gsb['Total_Bronze'] = df_gsb.iloc[:, 2] + df_gsb.iloc[:, 5] + df_gsb.iloc[:, 8] df_gsb['Points'] = df_gsb['Total_Gold'] * 3 + df_gsb[ 'Total_Silver'] * 2 + df_gsb['Total_Bronze'] * 1 return (df_gsb['Points'])
def q06_get_points(path): 'write your solution here' df = q02_rename_columns(path) a = df.iloc[:-1, 2].astype(int) aa = df.iloc[:-1, 7].astype(int) aaa = df.iloc[:-1, 12].astype(int) gold = (a + aa + aaa) * 3 b = df.iloc[:-1, 3].astype(int) bb = df.iloc[:-1, 8].astype(int) bbb = df.iloc[:-1, 13].astype(int) silver = (b + bb + bbb) * 2 c = df.iloc[:-1, 4].astype(int) cc = df.iloc[:-1, 9].astype(int) ccc = df.iloc[:-1, 14].astype(int) bronze = c + cc + ccc points = gold + silver + bronze df['Points'] = points return df['Points']