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")DICTIONARIES:
In Dictionaries no duplicate values are allowed , and data is stored in {key:value} format. the key opens the door to the value of that key.
We can also make dictionary from given lists for eg: countries = ["india", "albenia", "norway", "germany",] capitals = ["new delhi", "paris", "oslo", "berlin", ]
world_dict = { 'india':'new delhi', 'albenia':'paris', 'norway':'paris', 'germany':'berlin'} in the abpve example we can see that there is one key which has a correspondig value given to it.
we can also access values of a key by using index method, for eg:
world_dict['india'] Output: new delhi
keys are immutable objects.
DICTIONARYCEPTION:
the value of the keys can also be a dictionary, simply speaking a dictionary can also hold other dictionaries
#printing the index of gerany and finding the capital of germany
countries = ["india", "albenia", "france", "germany",]
capitals = ["madrid", "paris", "oslo", "berlin", "new delhi" ]
ind_ger = countries.index('germany') #here we got the index
#now for finding the capital of germany
print(capitals[ind_ger])
#accesing values of keys
world_dict= { 'india':'new delhi', 'albenia':'paris', 'norway':'oslo', 'germany':'berlin'}
print(world_dict['india'])
#to access all the keys of a dictionary
print(world_dict.keys())#to add a key value to a dictionary
world_dict= { 'india':'new delhi', 'albenia':'paris', 'norway':'oslo', 'germany':'berlin'}
#adding India as a key whose value is new delhi
world_dict['India'] = 'new delhi'
#print(world_dict)
#to remove a key value from a dictionary
#removing ablenia
del(world_dict['albenia'])
print(world_dict)
#dicitonary of dictionaries
foods = {
'indian': {'roti':'sabji', 'aloo':'gobhi', 'dhaniya':'pudina'},
'chinese': {'chowmien':'manchurian', 'friedrice':'noodles', 'momos':'springrolls'}
}
#adding a sub dictionary to foods dict
mughalayi = {'naan':'korma', 'zarda':'pulao', 'halim':'nihari'}
foods['mughal'] = mughalayi
print(foods)PANDAS:
developed by Wes Mckinney, pandas is a high level data manipulation tool which is built on numpy library.
DATAFRAMES:
DataFrame is one of Pandas's most important Data Structure. Its basicaaly a way to store data in tabular form where you can label the rows and columns. In Dataframes rows represent the observations and column represents the variables, Dataframes can be created by using different ways:
1.From using Dictionaries 2.By using CSV (Comma Separated Values) files
Indexing & Selecting Data:
Column Access [] :
#pandas using Dictionaries
import pandas as pd
#pre defined lists
countries = ['china','india','usa','russia']
safe = [False,True,True,False]
students = [20,70,200,100]
#creating a Dictionary from using lists
my_dict = {'X':countries,'Y':safe, 'Z':students}
education_data = pd.DataFrame(my_dict)
row_labels = ['a','b','c','d']
education_data.index = row_labels
print(education_data)#importing a csv file
import pandas as pd
countries = pd.read_csv("datasets/brics.csv")
print(countries)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
bricsDataFrame and prints "The population of {country} is {population} million!". - Create a histogram of the life expectancies for countries in Africa in the
gapminderDataFrame. 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!".