Last active
May 25, 2018 11:47
-
-
Save LalehT/6761f5ae88bf2af062a585145e4d892c to your computer and use it in GitHub Desktop.
Iran History
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
#Generating Years we want to study | |
Persian_years = list(range(1161,1397)) | |
y = str(len(Persian_years)) | |
print(y + ' years history of Iran') | |
type(Persian_years[1]) | |
#Generating the kings datastructures | |
Qhajar_kings_Gregorian = [['Agha Mohammad Khan' , pd.Timestamp(1789), pd.Timestamp(1797)] , 'Fath-Ali Shah (1797–1834)', 'Mohammad Shah (1834–1848)', | |
'Naser al-Din Shah (1848–1896)', 'Mozaffar al-Din Shah (1896–1907)', 'Mohammad Ali Shah (1907–1909)', 'Ahmad Shah (1909–1925)'] | |
Pahlavi_kings_Gregorian = [['Reza Shah Pahlavi', pd.Timestamp('1925.12.15'), pd.Timestamp('16 September 1941')], | |
['MohammadReza Shah Pahlavi', pd.Timestamp('16 September 1941'), pd.Timestamp('11 February 1979')]] | |
Constitutional_kings = Qhajar_kings_Gregorian[4:] + Pahlavi_kings_Gregorian #list manipulation | |
#Jalali / Persian Dates | |
Qhajar_kings_Jalali = [['Agha Mohammad Khan', 1161,1175] , ['Fath-Ali Shah',1176,1213], ['Mohammad Shah',1213,1226], | |
['Naser al-Din Shah',1226,1275], ['Mozaffar al-Din Shah', 1275,1285], ['Mohammad Ali Shah',1285,1288], | |
['Ahmad Shah',1288,1304]] | |
Qhajar_kings_Jalali[0:4] #list slicing: returns the first to the forth item in the list | |
Pahlavi_kings_Jalali = [['Reza Shah Pahlavi',1304,1320],['MohammadReza Shah Pahlavi',1320,1357]] | |
IR_leaders_Jalali = [['Khomeini',1357,1368],['Khamenei',1368,1396]] | |
#numpy array for element-wize calculations | |
#numpy assumes that all elements are the same type in contrast to list | |
np_2Darray = np.array(Pahlavi_kings_Gregorian) #2Dimentional array, array of list of list | |
date_end_RezaShah = np_2Darray[0][2] # same as np np_2Darray[0,2] : first row third column | |
np_2Darray[:,1:3] #all rows , second and thrid column | |
'''Review Data Structures''' | |
'''Series and DataFrames are data structures from pandas''' | |
#series can be thought of as one-dimentional array/list or a coulmn in a table but series can have lables | |
#Series can have elements of different types just like list , but in contrast to numpy arrays | |
#DataFrames can be defined as 2-dimentional labled data structures with columns of dfferent types | |
df = pd.DataFrame(Persian_years) | |
df.rename(columns={0:'Persian Year'},inplace=True) | |
df['Gregorian Year'] = 'convert' # Jalali to Gregorian | |
#Creating Dynasty Column | |
s_dynasty = pd.Series(index = Persian_years) | |
for i in s_dynasty.index: | |
if (i >= 1161) & (i <1304): | |
s_dynasty[i] = 'Qhajar' | |
elif (i >= 1304) & (i < 1357): | |
s_dynasty[i] = 'Pahlavi' | |
elif (i >= 1357) & (i <=1396): | |
s_dynasty[i] = 'Islamic Republic' | |
df_dynasty = pd.DataFrame(s_dynasty, columns=['Dynasty']) | |
df = df.join(df_dynasty, how='inner', on='Persian Year') | |
#Creating King Column | |
k_list = Qhajar_kings_Jalali + Pahlavi_kings_Jalali + IR_leaders_Jalali | |
df_kings = pd.DataFrame(k_list, columns=['King','Crowned Year', 'Ended']) | |
s_kings = pd.Series(index = Persian_years) | |
#filling the Series with the king name indexed by the year | |
for j in s_kings.index: | |
for k in df_kings.index: | |
if(j >= df_kings.iloc[k][1]) & (j <= df_kings.iloc[k][2]): | |
s_kings[j] = df_kings.iloc[k][0] | |
#Adding the king names Series to the DataFrame | |
df_k = pd.DataFrame(s_kings, columns=['King']) | |
df = df.join(df_k, how='inner', on='Persian Year') | |
#Creating PM (Prime Minister) DataFrame | |
'''!pip install --upgrade pip | |
!pip install --upgrade ipython jupyter | |
!pip install xlrd''' | |
import xlrd | |
df_PMs = pd.read_excel(io='Iran-PMs.xlsx') | |
df_PMs.drop('No.', axis = 1, inplace=True) | |
#df_PMs.set_index(['P-Year Entered Office','P-Year Left Office'],inplace= True) | |
#df['Prime Minister'] = P-Year Entered Office P-Year Left Office | |
df_PMs_Summary = df_PMs[[ 'Name', 'P-Year Entered Office','P-Year Left Office']] | |
#df_PMs_Summary[df_PMs_Summary.index == 1357]['Name'] | |
#df_PMs_Summary.loc[79]['Name'] #this returns 'Mir-Hossein Mousavi' and the type is str | |
# filling the Series with the PM name indexed by the year | |
s_PM = pd.Series(index = Persian_years) | |
string = '' | |
for i in s_PM.index: | |
for j in df_PMs_Summary.index: | |
if (i >= df_PMs.iloc[j]['P-Year Entered Office']) & (i <= df_PMs.iloc[j]['P-Year Left Office']): | |
string = string + df_PMs.iloc[j]['Name'] + ' , ' | |
s_PM[i] = string | |
string = '' | |
s_PM[120:210] #returns row 120 to 205 *** works for both Series and DataFrames *** | |
#Adding PM (Prime Minister) Column | |
df_p = pd.DataFrame(s_PM, columns=['Prime Ministers']).reset_index() | |
df_p.columns=['Persian Year', 'Prime Ministers'] | |
df = df.merge(df_p, how='inner', on='Persian Year') | |
df[120:210] | |
#Creating Important events DataFrame/List/Series | |
#Creating influential people DataFrame including gender which can later be queried for influential women |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment