Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
YStrano
GitHub Repository: YStrano/DataScience_GA
Path: blob/master/april_18/lessons/lesson-02/code/solution-code/solution-code-2.ipynb
1905 views
Kernel: Python 3

Solutions to Lesson 2

Lab 2 Solution

This is a quiz given in Roger Peng's Coursera class Computing for Data Analysis.

import pandas as pd import os data = pd.read_csv(os.path.join('..', '..', 'assets', 'dataset', 'ozone.csv'))
print data.head()
Ozone Solar.R Wind Temp Month Day 0 41 190 7.4 67 5 1 1 36 118 8.0 72 5 2 2 12 149 12.6 74 5 3 3 18 313 11.5 62 5 4 4 NaN NaN 14.3 56 5 5

Print the column names of the dataset to the screen, one column name per line.

for x in data.columns.values: print x
Ozone Solar.R Wind Temp Month Day

Extract the first 2 rows of the data frame and print them to the console. What does the output look like?

tmp = data.ix[0:1] # or data.head(2) print tmp.head()
Ozone Solar.R Wind Temp Month Day 0 41 190 7.4 67 5 1 1 36 118 8.0 72 5 2

How many observations (i.e. rows) are in this data frame?

print len(data)
153

Extract the last 2 rows of the data frame and print them to the console. What does the output look like?

tmp = data.tail(2) print tmp.head()
Ozone Solar.R Wind Temp Month Day 151 18 131 8.0 76 9 29 152 20 223 11.5 68 9 30

What is the value of Ozone in the 47th row?

print data.ix[46:48,]
Ozone Solar.R Wind Temp Month Day 46 21 191 14.9 77 6 16 47 37 284 20.7 72 6 17 48 20 37 9.2 65 6 18
print data.ix[47,'Ozone']
37.0

How many missing values are in the Ozone column of this data frame?

print data['Ozone'].isnull().sum() print len(data) - len(data['Ozone'].dropna())
37 37

What is the mean of the Ozone column in this dataset? Exclude missing values (coded as NA) from this calculation.

print data['Ozone'].mean()
42.1293103448

Extract the subset of rows of the data frame where Ozone values are above 31 and Temp values are above 90. What is the mean of "Solar.R" in this subset?

print data[(data.Ozone > 31) & (data.Temp > 90)].head()
Ozone Solar.R Wind Temp Month Day 68 97 267 6.3 92 7 8 69 97 272 5.7 92 7 9 119 76 203 9.7 97 8 28 120 118 225 2.3 94 8 29 121 84 237 6.3 96 8 30
print data[(data.Ozone > 31) & (data.Temp > 90)]['Solar.R'].mean()
212.8

What is the mean of "Temp" when "Month" is equal to 6?

print data[ data.Month==6 ].Temp.mean() print data[ data.Month==6 ]['Temp'].mean()
79.1 79.1

What was the maximum ozone value in the month of May (i.e. Month = 5)?

print data[ data.Month==5 ].Ozone.max()
115.0