Skip to content

Intermediate Python

Run the hidden code cell below to import the data used in this course.


1 hidden cell

Take Notes

Add notes about the concepts you've learned and code cells with code you want to keep.

Import cars data

import pandas as pd cars = pd.read_csv('cars.csv', index_col = 0) print(cars)

Print out drives_right value of Morocco

print(cars.loc['MOR', 'drives_right'])

Print sub-DataFrame

print(cars.loc[['RU','MOR'],['country','drives_right']])

Import cars data

import pandas as pd cars = pd.read_csv('cars.csv', index_col = 0)

Print out drives_right column as Series

print(cars.loc[:,'drives_right'])

Print out drives_right column as DataFrame

print(cars.loc[:,['drives_right']])

Print out cars_per_cap and drives_right as DataFrame

print(cars.loc[:,['cars_per_cap','drives_right']])


#Import cars data
import pandas as pd cars = pd.read_csv('cars.csv', index_col = 0) print(cars)

#Print out drives_right value of Morocco
print(cars.loc['MOR', 'drives_right'])

#Print sub-DataFrame
print(cars.loc[['RU','MOR'],['country','drives_right']])

#Import cars data
import pandas as pd cars = pd.read_csv('cars.csv', index_col = 0)

#Print out drives_right column as Series
print(cars.loc[:,'drives_right'])

#Print out drives_right column as DataFrame
print(cars.loc[:,['drives_right']])

#Print out cars_per_cap and drives_right as DataFrame
print(cars.loc[:,['cars_per_cap','drives_right']])



# Import cars data
import pandas as pd
cars = pd.read_csv('cars.csv', index_col = 0)

# Import numpy, you'll need this
import numpy as np

# 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)



# Import cars data
import pandas as pd
cars = pd.read_csv('cars.csv', index_col = 0)

# Code for loop that adds COUNTRY column

for lab, row in cars.iterrows():
    cars.loc[lab, 'COUNTRY'] = row['country'].upper()




# Print cars
print(cars)

Explore Datasets

Use the DataFrames imported in the first cell to explore the data and practice your skills!

  • Create a loop that iterates through the brics DataFrame and prints "The population of {country} is {population} million!".
  • Create a histogram of the life expectancies for countries in Africa in the gapminder DataFrame. Make sure your plot has a title, axis labels, and has an appropriate number of bins.
  • Simulate 10 rolls of two six-sided dice. If the two dice add up to 7 or 11, print "A win!". If the two dice add up to 2, 3, or 12, print "A loss!". If the two dice add up to any other number, print "Roll again!".