From 66a5572e72b6d43593cd12a00c748af91a5e51f4 Mon Sep 17 00:00:00 2001 From: Real Python Bot <42617967+realpython-bot@users.noreply.github.com> Date: Wed, 2 Dec 2026 20:08:16 +0000 Subject: [PATCH] Add python-pandas-tricks materials folder Sample code for the pandas tricks tutorial: the interpreter startup script (trick #1) and membership_map() (trick #7), pinned to pandas 3.1.2 / NumPy 2.5.5 and verified on Python 3.15.2. Co-Authored-By: Claude Fable 5.1 --- python-pandas-tricks/README.md | 39 ++++++++++++++++++++++++++ python-pandas-tricks/membership_map.py | 31 ++++++++++++++++++++ python-pandas-tricks/pandas_startup.py | 31 ++++++++++++++++++++ python-pandas-tricks/requirements.txt | 2 ++ 4 files changed, 103 insertions(+) create mode 100644 python-pandas-tricks/README.md create mode 100644 python-pandas-tricks/membership_map.py create mode 100644 python-pandas-tricks/pandas_startup.py create mode 100644 python-pandas-tricks/requirements.txt diff --git a/python-pandas-tricks/README.md b/python-pandas-tricks/README.md new file mode 100644 index 0000000000..8fc5b81409 --- /dev/null +++ b/python-pandas-tricks/README.md @@ -0,0 +1,39 @@ +# Python pandas: Tricks & Features You May Not Know + +This folder contains the sample code for the Real Python tutorial [Python pandas: Tricks & Features You May Not Know](https://realpython.com/python-pandas-tricks/). + +- `pandas_startup.py` configures pandas display options at interpreter startup (trick #1). +- `membership_map.py` bins the values of a Series by group membership with `Series.map()` (trick #7). + +## Setup + +You need Python 3.11 or later. Create and activate a virtual environment, then install the pinned dependencies: + +```console +$ python -m venv venv/ +$ source venv/bin/activate +(venv) $ python -m pip install -r requirements.txt +``` + +## Usage + +Point `PYTHONSTARTUP` at the startup script to launch an interpreter session with pandas imported and your display options set: + +```console +(venv) $ PYTHONSTARTUP=pandas_startup.py python +>>> pd.get_option("display.max_rows") +14 +``` + +Run the membership mapping example as a script: + +```console +(venv) $ python membership_map.py +0 North America +1 North America +2 North America +3 Europe +4 Europe +5 other +dtype: str +``` diff --git a/python-pandas-tricks/membership_map.py b/python-pandas-tricks/membership_map.py new file mode 100644 index 0000000000..bc26585517 --- /dev/null +++ b/python-pandas-tricks/membership_map.py @@ -0,0 +1,31 @@ +"""Bin the values of a Series by group membership with Series.map().""" + +from typing import Any + +import pandas as pd + + +def membership_map( + s: pd.Series, groups: dict, fillvalue: Any = -1 +) -> pd.Series: + # Reverse & expand the dictionary key-value pairs + groups = {x: k for k, v in groups.items() for x in v} + return s.map(groups).fillna(fillvalue) + + +if __name__ == "__main__": + countries = pd.Series( + [ + "United States", + "Canada", + "Mexico", + "Belgium", + "United Kingdom", + "Thailand", + ] + ) + groups = { + "North America": ("United States", "Canada", "Mexico", "Greenland"), + "Europe": ("France", "Germany", "United Kingdom", "Belgium"), + } + print(membership_map(countries, groups, fillvalue="other")) diff --git a/python-pandas-tricks/pandas_startup.py b/python-pandas-tricks/pandas_startup.py new file mode 100644 index 0000000000..d858a6e54a --- /dev/null +++ b/python-pandas-tricks/pandas_startup.py @@ -0,0 +1,31 @@ +"""Configure pandas display options at interpreter startup. + +Point the PYTHONSTARTUP environment variable at this file, and every +interactive session starts with pandas imported and these options set. +""" + +import pandas as pd + + +def start(): + options = { + "display": { + "max_columns": None, + "max_colwidth": 25, + "expand_frame_repr": False, # Don't wrap to multiple pages + "max_rows": 14, + "min_rows": 14, + "max_seq_items": 50, # Max length of printed sequence + "precision": 4, + "show_dimensions": False, + }, + } + + for category, option in options.items(): + for op, value in option.items(): + pd.set_option(f"{category}.{op}", value) + + +if __name__ == "__main__": + start() + del start # Clean up namespace in the interpreter diff --git a/python-pandas-tricks/requirements.txt b/python-pandas-tricks/requirements.txt new file mode 100644 index 0000000000..33f1d87505 --- /dev/null +++ b/python-pandas-tricks/requirements.txt @@ -0,0 +1,2 @@ +pandas==3.1.2 +numpy==2.5.5