Following code is given, we need to sum up the medals.
import pandas as pd
# Defining the three dataframes indicating the gold, silver, and bronze medal counts
# of different countries
gold = pd.DataFrame({'Country': ['USA', 'France', 'Russia'],
'Medals': [15, 13, 9]}
)
silver = pd.DataFrame({'Country': ['USA', 'Germany', 'Russia'],
'Medals': [29, 20, 16]}
)
bronze = pd.DataFrame({'Country': ['France', 'USA', 'UK'],
'Medals': [40, 28, 27]}
)
I wrote this working solution. However, it feels very un-pythonic: And I feel there is a better way of approaching this.
df = gold.set_index('Country')["Medals"].add(silver.set_index('Country')["Medals"], fill_value=0).add(bronze.set_index('Country')["Medals"], fill_value=0)
df = pd.DataFrame(df.sort_values(ascending=False))
print(df)```