In: Computer Science
Please use python.
Step a: Create two DataFrames df1 and
df2 from the following two tables that hold
student scores. Each DataFrame has four rows and
three columns.
df1: |
df2: |
|||||
Name |
Course A |
Course B |
Name |
Course C |
Course D |
|
Adam |
80 |
84 |
Bob |
65 |
72 |
|
Bob |
74 |
76 |
David |
85 |
82 |
|
David |
78 |
83 |
Eva |
76 |
80 |
|
Tom |
85 |
82 |
Tom |
90 |
88 |
Step b: Join the two DataFrames into df3 so that it only includes the students who appear in both tables. Print df3.
Step c: Set the column 'Name' as the index of df3 using df3.set_index() function. Print the updated df3. You can learn from https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.set_index.html.
Step d: Show the average score of each student in df3.
Code In Python:
import pandas as pd
#Step A : creation of two dataframes
data1 = {'Name':['Adam','Bob','David','Tom'] ,
'CourseA':[80,74,78,85], 'CourseB':[84,76,83,82]}
data2 ={'Name':['Bob','David','Eva','Tom'],
'CourseA':[65,85,76,90], 'CourseB':[72,82,80,88]};
df1 = pd.DataFrame(data1);
df2 = pd.DataFrame(data2);
#printing the two dataframes which are created in step A
print(df1)
print(df2)
#Step B: Join the two DataFrames into df3 so that it only includes
the students who appear in both tables.
df3 = pd.merge(df1,df2, how ='inner',on = ['Name']);
# print the dataframe created in step B.
print(df3)
#Step c: Set the column 'Name' as the index of df3 using
df3.set_index() function.
df3.set_index('Name',inplace=True);
print(df3);
#Step d: Show the average score of each student in df3.
df3=df3.mean(axis = 1,skipna = True);
print(df3)
This is the step wise output of the program: