import wooldridge as woo
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
Working with Jupyter Notebook
The following example is based on Script Descr-Figures
from Chapter 2 and demonstrates the use of Jupyter Notebooks to document your work step by step. We will describe the two most important building blocks:
- basic Markdown commands to format your text in
Markdown
cells - how to import and run Python code in
Code
cells
Import and Prepare Data
Let’s start by importing all external modules:
In the next step, we import our data and define important variables:
= woo.dataWoo('affairs')
affairs
# use a pandas.Categorical object to attach labels:
'haskids'] = pd.Categorical.from_codes(affairs['kids'],
affairs[=['no', 'yes'])
categories= affairs['haskids'].value_counts() counts
Analyse Data
View your Data
To get an overview you could use affairs.head()
.
Calculate Descriptive Statistics
Up to this point, the code cells above produced no output. This will change now, as we are interested in some results. Let’s start with printing out the average age. We start with its definition and use LaTeX to enter the equation: \[ \bar{x} = \frac{1}{N} \sum_{i=1}^N x_{i} \] The resulting Python code gives:
= np.mean(affairs['age'])
age_mean print(age_mean)
32.48752079866888
Produce Graphic Results
In Chapter 2, we saw how to produce a pie chart. Let’s repeat it here:
= plt.pie(counts, labels=['no', 'yes']) plot
You can also show Python code without executing it. You can use inline code
, or for longer paragraphs
'no', 'yes'], counts, color='dimgrey') plt.bar([