Skip to content
Intermediate Python
Intermediate Python
Run the hidden code cell below to import the data used in this course.
# Import the course packages
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Import the datasets
brics = pd.read_csv("datasets/brics.csv")
cars = pd.read_csv('datasets/cars.csv', index_col = 0)
gapminder = pd.read_csv("datasets/gapminder.csv")[1,2,3] * 3Take Notes
Add notes about the concepts you've learned and code cells with code you want to keep.
Add your notes here
Indexing, .loc[], and .iloc[]
# Import cars data (pandas imported above)
# import pandas as pd
cars = pd.read_csv('cars.csv', index_col = 0)
# Print out first 3 observations
print(cars[0:3])
# Print out fourth, fifth and sixth observation
print(cars.iloc[[3, 4, 5]])
# Other examples using .loc and .iloc
cars.loc['RU'] # single brackets returns a series
cars.iloc[4]
cars.loc[['RU']] # double brackets returns a df
cars.iloc[[4]]
cars.loc[['RU', 'AUS']]
cars.iloc[[4, 1]]
cars.loc['IN', 'cars_per_cap']
cars.iloc[3, 0]
cars.loc[['IN', 'RU'], 'cars_per_cap']
cars.iloc[[3, 4], 0]
cars.loc[['IN', 'RU'], ['cars_per_cap', 'country']]
cars.iloc[[3, 4], [0, 1]]Print series vs. dataframe -- use of single or double [], [[]]
# Print out drives_right column as Series
print(cars.iloc[:,2])
# Print out drives_right column as DataFrame
print(cars.loc[:, ['drives_right']])Filtering pandas DataFrames
cars = pd.read_csv('cars.csv', index_col = 0)
# Extract drives_right column as Series: dr
dr = cars['drives_right']
# Use dr to subset cars: sel
sel = cars[dr]
# Print sel
print(sel)
# Create car_maniac: observations that have a cars_per_cap over 500
cpc = cars['cars_per_cap']
car_maniac = cars[cpc > 500]
# Print car_maniac
print(car_maniac)
# Create medium: observations with cars_per_cap between 100 and 500
cpc = cars['cars_per_cap']
between = np.logical_and(cpc > 100, cpc < 500)
medium = cars[between]
# Print medium
print(medium)Loops, if, elif
# Initialize offset
offset = -6
''''
Inside the while loop, complete the if-else statement:
If offset is greater than zero, you should decrease offset by 1.
Else, you should increase offset by 1.
''''
# Code the while loop
while offset != 0 :
print("correcting...")
if offset > 0 :
offset = offset - 1
else :
offset = offset + 1
print(offset)
# Loop over list
# areas list
areas = [11.25, 18.0, 20.0, 10.75, 9.50]
# Code the for loop
for dim in areas:
print(dim)
# Change for loop to use enumerate() and update print()
for index, y in enumerate(areas) :
index = index + 1 # Starts index at 1 for counting comprehension
print("room " + str(index) + ": " + str(y))For loop accessing an list of lists
# house list of lists
house = [["hallway", 11.25],
["kitchen", 18.0],
["living room", 20.0],
["bedroom", 10.75],
["bathroom", 9.50]]
# Build a for loop from scratch
for x in house:
print("the " + x[0] + " is " + str(x[1]) + " sqm")