Exercise D: Pandas basics#

2021-8-24

import pandas as pd
import numpy as np

D.1 Create DataFrame from dict#

medals = {'Country': ['China','New Zealand','France'],
          'Gold': [38, 7, 10],
          'Silver': [32, 6, 12],
          'Bronze': [18, 7, 11],
          'Population': [1_439_323_776, 4_822_233, 65_273_511]}
# create a DataFrame df from the medals dict, print it
# set the index to 'Country'
# add a new column 'Total' with total number of medals per country
# add a new column 'Population_per_medal' (convert to np.int32)
# sort the DataFrame by "Population_per_medal"

D.2 Read DataFrame from csv/xlsx#

# read olympic-medals.csv with pd.read_csv() using index_col=0 as argument
# call the new DataFrame df2
# print the first 4 rows
# get the row of "Denmark"
# sort the teams alphabetically
# get the total number of Silver medals