Skip to content

Commit 977a9fe

Browse files
mmckyclaude
andauthored
[pandas_panel] Take programming's text, so both series publish one lecture (#1088)
* [pandas_panel] Take programming's text, so both series publish one lecture Programming and intermediate both publish pandas_panel. The two copies were the same lecture drifting apart: programming's carries the pandas 3.0 update and later tidy-ups that never reached this copy. John and Matt decided on 2026-10-12 to publish one text (project-monorepo#110), so this copy becomes programming's, byte for byte. Programming's text links to its pandas lecture with {doc}`pandas`, which this site does not have. intersphinx_disabled_reftypes: [] lets a reference with no local target resolve through intersphinx, here to python-programming.quantecon.org. Every other reference in this series already resolves locally (the build runs -n -W), so nothing else moves. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [pandas_panel] Describe the single .replace() call, not a loop The sentence above the continent recoding still said we "loop through a list", but the code makes one .replace() call with the whole list. Say what the code does, and end the sentence with a full stop. Programming takes the identical change, so the two copies stay byte-identical. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 35832b1 commit 977a9fe

2 files changed

Lines changed: 33 additions & 26 deletions

File tree

‎lectures/_config.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ sphinx:
9494
sticky_contents: true
9595
launch_buttons:
9696
colab_url : https://colab.research.google.com
97+
# A reference with no target in this site resolves on the lecture's home
98+
# site (pandas_panel, shared with programming, links to its pandas lecture).
99+
intersphinx_disabled_reftypes: []
97100
intersphinx_mapping:
98101
intro:
99102
- "https://intro.quantecon.org/"

‎lectures/pandas_panel.md‎

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ kernelspec:
1111
name: python3
1212
---
1313

14-
(ppd)=
1514
```{raw} jupyter
1615
<div id="qe-notebook-header" align="right" style="text-align:right;">
1716
<a href="https://quantecon.org/" title="quantecon.org">
@@ -20,18 +19,31 @@ kernelspec:
2019
</div>
2120
```
2221

22+
(ppd)=
2323
# {index}`Pandas for Panel Data <single: Pandas for Panel Data>`
2424

2525
```{index} single: Python; Pandas
2626
```
2727

28-
```{contents} Contents
29-
:depth: 2
28+
In addition to what’s in Anaconda, this lecture will need the following libraries:
29+
30+
```{code-cell} ipython3
31+
:tags: [hide-output]
32+
33+
!pip install --upgrade seaborn
34+
```
35+
36+
We use the following imports.
37+
38+
```{code-cell} ipython3
39+
import matplotlib.pyplot as plt
40+
import seaborn as sns
41+
sns.set_theme()
3042
```
3143

3244
## Overview
3345

34-
In an [earlier lecture on pandas](https://python-programming.quantecon.org/pandas.html), we looked at working with simple data sets.
46+
In an {doc}`earlier lecture on pandas <pandas>`, we looked at working with simple data sets.
3547

3648
Econometricians often need to work with more complex data sets, such as panels.
3749

@@ -65,7 +77,7 @@ countries and assign it to `realwage`.
6577
The dataset can be accessed with the following link:
6678

6779
```{code-cell} ipython3
68-
url1 = 'https://github.com/QuantEcon/data-lectures/raw/main/lectures/realwage.csv'
80+
url1 = 'https://raw.githubusercontent.com/QuantEcon/data-lectures/main/lectures/realwage.csv'
6981
```
7082

7183
```{code-cell} ipython3
@@ -138,14 +150,14 @@ the row index (`.unstack()` works in the opposite direction - try it
138150
out)
139151

140152
```{code-cell} ipython3
141-
realwage.stack(future_stack=True).head()
153+
realwage.stack().head()
142154
```
143155

144156
We can also pass in an argument to select the level we would like to
145157
stack
146158

147159
```{code-cell} ipython3
148-
realwage.stack(level='Country', future_stack=True).head()
160+
realwage.stack(level='Country').head()
149161
```
150162

151163
Using a `DatetimeIndex` makes it easy to select a particular time
@@ -155,7 +167,7 @@ Selecting one year and stacking the two lower levels of the
155167
`MultiIndex` creates a cross-section of our panel data
156168

157169
```{code-cell} ipython3
158-
realwage.loc['2015'].stack(level=(1, 2), future_stack=True).transpose().head()
170+
realwage.loc['2015'].stack(level=(1, 2)).transpose().head()
159171
```
160172

161173
For the rest of lecture, we will work with a dataframe of the hourly
@@ -185,7 +197,7 @@ function.
185197
The dataset can be accessed with the following link:
186198

187199
```{code-cell} ipython3
188-
url2 = 'https://github.com/QuantEcon/data-lectures/raw/main/lectures/countries.csv'
200+
url2 = 'https://raw.githubusercontent.com/QuantEcon/data-lectures/main/lectures/countries.csv'
189201
```
190202

191203
```{code-cell} ipython3
@@ -232,7 +244,6 @@ a corresponding data entry `realwage_f`.
232244
This is illustrated by the red shading in the following diagram
233245

234246
```{figure} /_static/lecture_specific/pandas_panel/venn_diag.png
235-
236247
```
237248

238249
We will also need to specify where the country name is located in each
@@ -294,14 +305,11 @@ merged[merged['Country'] == 'Korea']
294305

295306
We will also combine the Americas into a single continent - this will make our visualization nicer later on.
296307

297-
To do this, we will use `.replace()` and loop through a list of the continent values we want to replace
308+
To do this, we will use `.replace()` with a list of the continent values we want to replace.
298309

299310
```{code-cell} ipython3
300311
replace = ['Central America', 'North America', 'South America']
301-
302-
for country in replace:
303-
merged.Continent = merged.Continent.replace(to_replace=country,
304-
value='America')
312+
merged['Continent'] = merged['Continent'].replace(to_replace=replace, value='America')
305313
```
306314

307315
Now that we have all the data we want in a single `DataFrame`, we will
@@ -361,12 +369,6 @@ merged.mean().head(10)
361369
Using this series, we can plot the average real minimum wage over the
362370
past decade for each country in our data set
363371

364-
```{code-cell} ipython3
365-
import matplotlib.pyplot as plt
366-
import seaborn as sns
367-
sns.set_theme()
368-
```
369-
370372
```{code-cell} ipython3
371373
merged.mean().sort_values(ascending=False).plot(kind='bar',
372374
title="Average real minimum wage 2006 - 2016")
@@ -397,10 +399,12 @@ plt.show()
397399
```
398400

399401
We can also specify a level of the `MultiIndex` (in the column axis)
400-
to aggregate over
402+
to aggregate over.
403+
404+
In the case of `groupby`, we need to use `.T` to transpose the columns into rows, as `pandas` has removed support for `axis=1` in the `groupby` method.
401405

402406
```{code-cell} ipython3
403-
merged.T.groupby(level='Continent').mean().T.head()
407+
merged.T.groupby(level='Continent').mean().head()
404408
```
405409

406410
We can plot the average minimum wages in each continent as a time series
@@ -428,7 +432,7 @@ plt.show()
428432
summary statistics
429433

430434
```{code-cell} ipython3
431-
merged.stack(future_stack=True).describe()
435+
merged.stack().describe()
432436
```
433437

434438
This is a simplified way to use `groupby`.
@@ -502,7 +506,7 @@ in Europe by age and sex from [Eurostat](https://ec.europa.eu/eurostat/data/data
502506
The dataset can be accessed with the following link:
503507

504508
```{code-cell} ipython3
505-
url3 = 'https://github.com/QuantEcon/data-lectures/raw/main/lectures/employ.csv'
509+
url3 = 'https://raw.githubusercontent.com/QuantEcon/data-lectures/main/lectures/employ.csv'
506510
```
507511

508512
Reading in the CSV file returns a panel dataset in long format. Use `.pivot_table()` to construct
@@ -617,4 +621,4 @@ plt.show()
617621
```
618622

619623
```{solution-end}
620-
```
624+
```

0 commit comments

Comments
 (0)