Skip to content

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 two datasets
gapminder = pd.read_csv("datasets/gapminder.csv")
brics = pd.read_csv("datasets/brics.csv")

Take Notes

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

Pandas commands:

  • pd.DataFrame(dictionary)
  • dataFrame.index is used for put a name to the columns. We can create a list with these names and assign it to dataFrame.index like this: dataFrame.index = the_list
  • pd.read_csv('path to file')
  • pd.read_csv('path to file', index_col = 0). index_col = 0 is used to avoid the column with numbers of row appear in the DataFrame

Numpy Commands

  • np.array([list])
  • np.mean(arr)
  • np.median(arr)
  • np.std(arr)
  • np.logical_and(x, y)
  • np.logical_or(x, y)
  • np.logical_not(x, y)

list = list of values. arr = numpy array x, y = numpy arrays

Add your notes here

# Add your code snippets here

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!".
for lab, row in brics.iterrows():
    print(f"The population of {row['country']} is {row['population']} million!")
plt.hist(gapminder['life_exp'], bins=15)
plt.xlabel('Years of life')
plt.ylabel('Population')
plt.title('Life Expectancies for Coutries in Africa')
plt.show()
np.random.seed(123)
for i in range(100):
    dice_1 = np.random.randint(1, 7)
    dice_2 = np.random.randint(1, 7)
    if (dice_1 + dice_2) == 11:
        print('A win!')
    elif (dice_1 + dice_2) == 2 or (dice_1 + dice_2) == 3 or (dice_1 + dice_2) == 12:
        print('A loss!')
    else:
        print('Roll Again!')