Skip to content
Intermediate Python
  • AI Chat
  • Code
  • Report
  • Intermediate Python

    Histogram

    • Explore dataset
    • Get idea about distribution

    Matplotlib

    import matplotlib.pyplot as plt

    import matplotlib.pyplot as plt
    help(plt.hist)
    Hidden output
    values = [0,0.6,1.4,1.6,2.2,2.5,2.6,3.2,3.5,3.9,4.2,6]
    plt.hist(values, bins = 3)
    plt.show()

    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!".
    # 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")
    
    

    Data visualization

    • Many Options
    1. Different plot types
    2. Many customizations
    • Choice depends on
    1. Data
    2. Story you want to tell
    # Basic plot
    import matplotlib.pyplot as plt
    year = [1950,1951,1952, 2100]
    pop = [2.538, 2.57, 2.62 , 10.05]
    
    #Add more data
    year = [1800, 1850, 1900] + year
    pop = [1.0, 1.262, 1.650] + pop
    
    plt.plot(year, pop)
    
    plt.xlabel('Year')
    plt.ylabel('Population')
    plt.title('World Population Projections')
    plt.yticks([0,2,4,6,8,10],
               ['0','2B','4B','6B','8B','10B'])
    plt.show()

    Dictionaries

    # List
    
    pop = [30.55,2.77,39.21]
    countries = ["afghanistan", "albania", "algeria"]
    ind_alb = countries.index("albania")
    ind_alb
    pop[ind_alb]
    • Not convenient
    • Not intuitive
    # Dictionary
    pop = [30.55,2.77,39.21]
    countries = ["afghanistan", "albania", "algeria"]
    
    ...
    
    world = {"afghanistan":30.55, "albania":2.77, "algeria":39.21}
    world["albania"]
    
    # Definition of dictionary
    europe = {'spain':'madrid', 'france':'paris', 'germany':'berlin', 'norway':'oslo' }
    
    # Print out the keys in europe
    
    print(europe.keys())
    
    # Print out value that belongs to key 'norway'
    
    print(europe['norway'])
    # Recap
    world = {"afghanistan":30.55, "albania":2.77, "algeria":39.21}
    world["albania"]
    world = {"afghanistan":30.55, "albania":2.77, "algeria":39.21, "albania":2.81}
    world