Here are some quick references to Python Dictionaries and Panda include
#Dictionaries
#Create
cities_in_europe = {'spain':'madrid', 'france':'paris', 'germany':'berlin', 'norway':'oslo' }
#Read value back
cities_in_europe['france']
#Add to Dictionaries
cities_in_europe['italy'] = 'venice'
#Change a value
cities_in_europe['italy'] = 'rome'
#"key" in dicarr
'italy' in delcities_in_europe #-> returns true
#delete a key
del(cities_in_europe['italy'])
print(cities_in_europe)
#N Dimention Dictionaries
europe = { 'spain': { 'capital':'madrid', 'population':42.33 },
'france': { 'capital':'paris', 'population':63.02 },
'germany': { 'capital':'berlin', 'population':78.62 },
'norway': { 'capital':'oslo', 'population':8.078 } }
#Get Single value
europe['france']['capital']
europe['france']['population']
# Print out the capital of France
print(europe['france']['capital'])
# Add One more Element
data = {'capital':'rome','population':59.83}
# Add data to europe under key 'italy'
europe['italy'] = data
#Add in one line
europe['england'] = {'capital':'london','population':78.88}
# Print europe
print(europe)
# Pre-defined lists
names = ['United States', 'Australia', 'Japan', 'India', 'Russia', 'Morocco', 'Egypt']
dr = [True, False, False, False, True, True, True]
cpc = [809, 731, 588, 18, 200, 70, 45]
import pandas as pd
my_dict = {'country':names ,
'drives_right':dr ,
'cars_per_cap':cpc }
# Build a DataFrame cars from my_dict: cars
cars = pd.DataFrame(my_dict)
print(cars)
row_labels = ['US', 'AUS', 'JAP', 'IN', 'RU', 'MOR', 'EG']
# Specify row labels of cars
cars.index = row_labels
print(cars)
———- CVS FILE ————–
,country,capital,area,population
BR,Brazil,Brasilia,8.516,200.4
RU,Russia,Moscow,17.10,143.5
IN,India,New Delhi,3.286,1252
CH,China,Beijing,9.597,1357
SA,South Africa,Pretoria,1.221,52.98
import pandas as pd
brics = pd.read_csv("path/to/brics.csv", index_col = 0)
print(brics)
Column and Row Access
print(brics['country'])
print(type(brics['country']))
#Gives you <class 'pandas.core.series.Series'>
print(brics[['country']])
print(type(brics[['country']]))
#Gives you <class 'pandas.core.frame.DataFrame'>
#Two Columns
print(brics[['country','capital']])
#Row Access
brics[0:3] # Rows 0,1,2
brics[3:6] # Rows 3,4,5
#loc and iloc
brics.loc['RU'] #Selects row for RU
brics.iloc[1]
brics.loc[['BR','SA']] #Selects ROW BR and SA
brics.iloc[[0,4]]
#Select Area for SA
brics.loc['SA','area'])
# Capital and area for SA and RU
brics.loc[['SA','RU'],['capital','area']])
brics.loc[['RU','MOR'],['country','area']]
#Combine loc and iloc using ix
brics.ix[[4,5],['country','capital']]
brics.ix[4:6,['country','capital']]
#Slice-ing loc
brics.loc[:,'area']) #As Series
brics.loc[:,['area']]) #As DataFrame
brics.loc[:,['area','capital']]) #As DataFrame Area , Capital