Informatics Practices
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'.
Python Pandas
1 Like
Answer
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
Answered By
3 Likes
Related Questions
Give two identical DataFrames Sales16 and Sales17. But Sales17 has some values missing. Write code so that Sales17 fills its missing values from the corresponding entries of Sales16.
Write code that just produces single True/False as a result for the presence of missing values in whole DataFrame namely df.
Given a DataFrame that stores the details of past 25 years' monthly sales. Some old data is however missing. Write a script to calculate average :
Monthly sales across years
Yearly sales
Make sure that missing values do not hamper the overall result.
Given two identical DataFrames Sales16 and Sales17. But Sales17 has some values missing. Write code so that Sales17 fills its missing values from corresponding entries of Sales16.