<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Basic data IO and analysis
First, we need to import all the necessary libraries and set up some environment variables.
Step1:  Load the zip file from the web and save it to your hard drive.
Step2:  Show contents of the zip file.
Step3:  Read csv-formatted data directly from the zip file into pandas DataFrame. Also rename some columns for prettier output.
Step4:  Show unique values of the Topic column.
Step5:  Leave only those rows that have Expenditures in the column Topic. Next, leave only those that contain PPP in the Indicator Name column values. Finally, create a dictionary with a pair of variable key and its meaningful name.
Step6:  Do the same for Attainment among Topic values and slightly more involved subset of Indicator Name. Here we require that it contains both strings, with primary schooling and 15.
Step7:  Now show all column names in the primary data set.
Step8:  Combine two dictionaries into one.
Step9:  Subset the data to include only three interesting columns that we have found above and only for the year 2010.
Step10:  Export data to Excel.
Step11:  Now suppose we already have the data saved in the Excel file. Let's read it from scratch into pandas DataFrame.
Step12:  Let's see how percentage of educated population depends on government expenditures on primary students. Also, save the picture to the pdf file.
Step13:  To be more precise we can quantify the effect of expenditures on schooling via simple OLS regression.
Step14:  And save the key result to the LaTeX table.
<ASSISTANT_TASK:>
Python Code:
import re
import requests
import zipfile
import numpy as np
import pandas as pd
import matplotlib.pylab as plt
import seaborn as sns
import statsmodels.formula.api as sm
sns.set_context('talk')
pd.set_option('float_format', '{:6.2f}'.format)
%matplotlib inline
Explanation: Basic data IO and analysis
First, we need to import all the necessary libraries and set up some environment variables.
End of explanation
url = 'http://databank.worldbank.org/data/download/Edstats_csv.zip'
path = '../data/WorldBank/Edstats_csv.zip'
response = requests.get(url)
with open(path, "wb") as file:
    file.write(response.content)
Explanation: Load the zip file from the web and save it to your hard drive.
End of explanation
zf = zipfile.ZipFile(path)
files = zf.namelist()
print(files)
Explanation: Show contents of the zip file.
End of explanation
data = pd.read_csv(zf.open(files[0]))
series = pd.read_csv(zf.open(files[2]))
series.rename(columns={series.columns[0]: 'Series Code'}, inplace=True)
data.rename(columns={data.columns[0]: 'Country Name'}, inplace=True)
print(series.columns)
Explanation: Read csv-formatted data directly from the zip file into pandas DataFrame. Also rename some columns for prettier output.
End of explanation
print(series['Topic'].unique())
Explanation: Show unique values of the Topic column.
End of explanation
subset = series.query("Topic == 'Expenditures'")[['Series Code', 'Indicator Name']]
subset = subset[subset['Indicator Name'].str.contains('PPP')]
print(subset.values)
xvar = {'UIS.XUNIT.PPP.1.FSGOV': 'Expenditure per student'}
Explanation: Leave only those rows that have Expenditures in the column Topic. Next, leave only those that contain PPP in the Indicator Name column values. Finally, create a dictionary with a pair of variable key and its meaningful name.
End of explanation
subset = series.query("Topic == 'Attainment'")[['Series Code', 'Indicator Name']]
subset = subset[subset['Indicator Name'].str.contains('(?=.*with primary schooling)(?=.*15)')]
print(subset.values)
yvar = {'BAR.PRM.CMPT.15UP.ZS': 'Pct with schooling'}
Explanation: Do the same for Attainment among Topic values and slightly more involved subset of Indicator Name. Here we require that it contains both strings, with primary schooling and 15.
End of explanation
print(data.columns)
Explanation: Now show all column names in the primary data set.
End of explanation
renames = xvar.copy()
renames.update(yvar)
print(renames)
Explanation: Combine two dictionaries into one.
End of explanation
cols = ['Country Name', 'Indicator Code', '2010']
data_sub = data.ix[data['Indicator Code'].isin(renames.keys()), cols].dropna()
data_sub.replace({'Indicator Code': renames}, inplace=True)
data_sub.set_index(cols[:2], inplace=True)
data_sub = data_sub[cols[-1]].unstack(cols[1]).dropna()
data_sub.columns.name = 'Indicator'
data_sub.index.name = 'Country'
print(data_sub.head())
Explanation: Subset the data to include only three interesting columns that we have found above and only for the year 2010.
End of explanation
data_sub.to_excel('../data/WorldBank/education.xlsx', sheet_name='data')
Explanation: Export data to Excel.
End of explanation
education = pd.read_excel('../data/WorldBank/education.xlsx', sheet_name='data', index_col=0)
print(education.head())
Explanation: Now suppose we already have the data saved in the Excel file. Let's read it from scratch into pandas DataFrame.
End of explanation
education['Expenditure per student (log)'] = np.log(education['Expenditure per student'])
fig = plt.figure(figsize=(8, 6))
sns.regplot(x='Expenditure per student (log)', y='Pct with schooling',
            data=education, ax=fig.gca())
plt.savefig('../plots/education.pdf')
plt.show()
Explanation: Let's see how percentage of educated population depends on government expenditures on primary students. Also, save the picture to the pdf file.
End of explanation
formula = 'Q("Pct with schooling") ~ np.log(Q("Expenditure per student"))'
result = sm.ols(formula=formula, data=education).fit()
print(result.summary())
Explanation: To be more precise we can quantify the effect of expenditures on schooling via simple OLS regression.
End of explanation
out = pd.DataFrame({'Parameter': result.params, 't-stat': result.tvalues})
out.to_latex('../tables/education_ols.tex')
print(out)
Explanation: And save the key result to the LaTeX table.
End of explanation
<END_TASK>