Informatics Practices
A series object (say T1) stores the average temperature recorded on each day of a month. Write code to display the temperatures recorded on :
(i) first 7 days
(ii) last 7 days.
Answer
import pandas as pd
T1 = pd.Series([25.6, 26.3, 27.9, 28.2, 29.1, 30.9, 31.2, 32.4, 33.2, 34.4, 33.3, 32.5, 31.4, 30.7, 29.6, 28.9, 27.0, 26.2, 25.32, 24.34, 23.4, 22.3, 21.6, 20.9, 19.8, 18.1, 17.2, 16.34, 15.5, 14.6])
first_7_days = T1.head(7)
print("Temperatures recorded on the first 7 days:")
print(first_7_days)
last_7_days = T1.tail(7)
print("\nTemperatures recorded on the last 7 days:")
print(last_7_days)Output
Temperatures recorded on the first 7 days:
0 25.6
1 26.3
2 27.9
3 28.2
4 29.1
5 30.9
6 31.2
dtype: float64
Temperatures recorded on the last 7 days:
23 20.90
24 19.80
25 18.10
26 17.20
27 16.34
28 15.50
29 14.60
dtype: float64
Related Questions
Write Python code to create a Series object Temp1 that stores temperatures of seven days in it. Take any random seven temperatures.
Write Python code to create a Series object Temp2 storing temperatures of seven days of week. Its indexes should be 'Sunday', 'Monday',… 'Saturday'.
Series objects Temp1, Temp2, Temp3, Temp4 store the temperatures of days of week1, week2, week3, week4 respectively.
Write a script to
(a) print the average temperature per week.
(b) print average temperature of entire month.
Ekam, a Data Analyst with a multinational brand has designed the DataFrame df that contains the four quarters' sales data of different stores as shown below :
Store Qtr1 Qtr2 Qtr3 Qtr4 0 Store1 300 240 450 230 1 Store2 350 340 403 210 2 Store3 250 180 145 160 Answer the following questions :
(i) Predict the output of the following Python statement :
(a) print(df.size) (b) print(df[1:3])(ii) Delete the last row from the DataFrame.
(iii) Write Python statement to add a new column Total_Sales which is the addition of all the 4 quarter sales.