Computer scienceData scienceInstrumentsPandasStoring data with pandas

Combining Data in Pandas

School grades

Report a typo

Take a look at the following two DataFrames. They contain information about average grades at the school exams in 2019 and 2020, respectively.

grades_2019 = pd.DataFrame({'Subject': ['Physics', 'Geometry', 'Chemistry'], 'Average': ['68', '78', '75']})
grades_2020 = pd.DataFrame({'Subject': ['Physics', 'Geometry', 'Chemistry'], 'Average': ['72', '80', '75']})

Create a new DataFrame called grades_union from these two. To do this, concatenate grades_2019 and grades_2020 vertically and add additional hierarchical indexes: 'Year 2019' against the rows from grades_2019 and 'Year 2020' against the rows from grades_2020. You do NOT need to print the results.

Write a program in Python 3
import pandas as pd


grades_2019 = pd.DataFrame({'Subject': ['Physics', 'Geometry', 'Chemistry'], 'Average': ['68', '78', '75']})
grades_2020 = pd.DataFrame({'Subject': ['Physics', 'Geometry', 'Chemistry'], 'Average': ['72', '80', '75']})
grades_union = ...
___

Create a free account to access the full topic