CBSE Class 12 Informatics Practices Question 98 of 101

Python Pandas — II — Question 7

Back to all questions
7
Question

Question 7

Four Series objects Temp1, Temp2, Temp3 and Temp4 store the temperatures of week1, week2, week3 and week4 respectively. Create a DataFrame from these four series objects where the indexes should be 'Sunday', 'Monday', ... , 'Saturday', and columns should be 'Week1', 'Week2', 'Week3' and 'week4'.

Solution
import pandas as pd
weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
week1 = [25.4, 26.7, 27.2, 28.9, 29.8, 30.2, 31.5]  
week2 = [22.3, 23.2, 24.6, 25.4, 26.8, 27.9, 28.4]  
week3 = [20.1, 21.2, 22.4, 23.9, 24.7, 25.57, 26.78]  
week4 = [18.4, 19.1, 20.3, 21.7, 22.67, 23.4, 24.09]  
Temp1 = pd.Series(week1, index=weekdays)
Temp2 = pd.Series(week2, index=weekdays)
Temp3 = pd.Series(week3, index=weekdays)
Temp4 = pd.Series(week4, index=weekdays)
df = pd.DataFrame({'Week1': Temp1, 'Week2': Temp2, 'Week3': Temp3, 'Week4': Temp4})
print(df)
Output
           Week1  Week2  Week3  Week4
Sunday      25.4   22.3  20.10  18.40
Monday      26.7   23.2  21.20  19.10
Tuesday     27.2   24.6  22.40  20.30
Wednesday   28.9   25.4  23.90  21.70
Thursday    29.8   26.8  24.70  22.67
Friday      30.2   27.9  25.57  23.40
Saturday    31.5   28.4  26.78  24.09
Answer