Reuven M. Lerner
banner
lernerpython.com
Reuven M. Lerner
@lernerpython.com
Helping you become more confident with Python and Pandas since 1995: Books, videos, newsletters, corporate training, conference talks, and dad jokes.
Pinned
Hi there! 👋 I'm Reuven, and I've been teaching #Python and Pandas around the world since 1995.

Just about every day, my students ask great questions. I love sharing those questions (and my answers), along with other resources to help you improve your Python/Pandas fluency.
Want to add emojis to your #Python program's output? Rich again comes to the rescue:

import rich
rich.print('Have a good day! :smile:')

Just use :NAME: for the emoji you want. I usually use the "shortcodes" part of Emojipedia's "technical" tab.
November 16, 2026 at 4:31 PM
Want to make your #Python program's output colorful? Rich comes to the rescue:

import rich
rich.print('Hello [blue on yellow]out there[/blue on yellow]!')

rich.print is a superset of the "print" builtin, but lets you add styling. Some people even "from rich import print".
November 15, 2026 at 4:30 PM
Want to restrict user input to a list of acceptable choices in your #Python program? Pass the "choices" kwarg to Rich's Prompt.ask:

from rich.prompt import Prompt
user_is_happy = Prompt.ask('Are you happy?', choices=['yes', 'no'])

Bad input? The user has to try again.
November 14, 2026 at 4:30 PM
Tired of invoking "strip" when getting user input in #Python? Use the "Prompt.ask" method in Rich on PyPI:

from rich.prompt import Prompt
name = Prompt.ask('Enter your name')

It adds a colon and strips the user's input -- convenient and useful!
November 14, 2026 at 4:15 PM
In #Python #Pandas 3.1, reading CSV is multi-threaded, using up to 6 by default.

You can change it temporarily with mode.max_threads:

with pd.option_context('mode.max_threads', 10):
df = pd.read_csv('bigfile.csv')

Note: More threads aren't always faster!
November 12, 2026 at 4:30 PM
#VibeCoding is fun! But without a software background, how do you know your app is secure? Tested? Backed up?

Today I’m launching Vibe to Valid: I audit vibe-coded #Python apps, tell you what’s wrong (and right), and can fix it.

Free intro call: buff.ly/0Q6lgtk
November 12, 2026 at 3:02 PM
Want to set an option in #Python #Pandas, but just temporarily? You can use option_context:

with pd.option_context('display.precision', 2):
print(s/3)

Within the "with" block, the display precision is 2. But outside -- before or after -- it is the default.
November 11, 2026 at 5:29 PM
There are tons of config options in #Python #Pandas. Set them with pd.set_option:

pd.set_option('display.precision', 2)

Now floats in a series or data frame will have 2 digits after the decimal.

Retrieve the value with "get_option":

pd.get_option('display.precision')
November 10, 2026 at 4:30 PM
Reading a CSV into a #Python #Pandas data frame?

Before version 3.1, loading CSV was single-threaded and slow. My test file took 1.03s.

In 3.1, it's now multi-threaded and much faster, taking 252 ms.

However, it's still far slower than PyArrow, which took 25.5 ms.
November 9, 2026 at 4:30 PM
My newest course, Agentic coding with #ClaudeCode, is out!

• 4 hours of video
• Hands-on exercises
• Practical advice from my personal coding experience

It's included in a LernerPython membership. Check it out (with a 2-week free trial) at buff.ly/ET6nSz1 !
Go beyond syntax.
I've been teaching Python for 30+ years. LernerPython gives you the deep understanding, serious exercises, and direct access to me that tutorials and videos can't. Try it free for 14 days.
buff.ly
November 8, 2026 at 6:06 PM
Starting with #Python #Pandas 3.1, use "filter" to apply a condition to rows. So:

(
df
.loc[ pd.col('passenger_count') > 1 , 'trip_distance']
.mean()
)

can now be (the much slower):

(
df
.filter(cond=pd.col('passenger_count') > 1)
['trip_distance']
.mean()
)
November 8, 2026 at 4:30 PM
Given a #Python #Pandas data frame, you can add a new column with "assign":

df.assign(xy = pd.col('x') * pd.col('y'))

"select" in Pandas 3.1 treats kwargs like assign, dropping columns you don't name:

df.select('x', 'y', 'z',
xy = pd.col('x') * pd.col('y'))
November 7, 2026 at 4:30 PM
Want to retrieve specific columns from your #Python #Pandas data frame? In the past, you would say

df[['x', 'y']]

But now, with version 3.1, you can use the "select" method:

df.select('x', 'y')
November 6, 2026 at 4:30 PM
The comprehension-unpacking syntax in Python 3.15 is great, but you cannot transform the unpacked elements.

Given:

[[10, 20, 30], [40, 50, 60], [70, 80]]

We can get the individual numbers, but cannot run str on each. For that, we still need a nested comprehension.
November 5, 2026 at 5:03 PM
In #Python 3.15, get the unique elements in a list of iterables with the new in-comprehension unpacking syntax to create a set comprehension:

x = [[10, 20], [20, 30, 40], [40, 50, 60]]

{*n for n in x} # returns {10, 20, 30, 40, 50, 60}
November 4, 2026 at 4:42 PM
Did you hear about the $6b Binance hack?

In the latest Bamboo Weekly, we look at the history of crypto hacks with #Python #Pandas, learning whether such thefts are increasing over time — and how much, on average, the thieves get.

Level up your data analysis skills every week at buff.ly/f9mDmTb
November 4, 2026 at 4:27 PM
Turn a list of #dicts into one dict with #Python 3.15's unpacking and a dict comprehension:

dicts = [{'a':10, 'b':20}, {'c':30}, {'d':40, 'e':50}]
{**d for d in dicts} # {'a': 10, 'b': 20, 'c': 30, 'd': 40, 'e': 50}

Note: If two dicts share a key, the latter wins.
November 3, 2026 at 5:16 PM
Want to unroll a nested list in #Python 3.15?

x = [[10, 20, 30], [40, 50, 60], [70, 80]]

Use a nested list comprehension:

[n
for sublist in x
for n in sublist]

Or use the new unpacking syntax:

[*n for n in x] # same thing!
November 2, 2026 at 4:57 PM
My students used to ask if #Python #Pandas supports 3D data structures.

Answer: No. Series are 1D and data frames are 2D.

But... a multi-index effectively gives you more dimensions. You can then navigate the indexes with .loc and .xs.
November 1, 2026 at 4:30 PM
I've taught programming for 30+ years. After a year of coding with AI, I haven't defined one data structure.

Wires → assembly → C → #Python → AI: each abstraction let more people solve bigger problems.

What should we teach now?

buff.ly/MizDkzt
AI is programming's next abstraction. What should we teach? - LernerPython
Programming has climbed from wires to assembly to Python. AI coding is the next layer of abstraction, and it changes what new coders need to learn.
buff.ly
November 1, 2026 at 3:27 PM
To remove a level from a #Python #Pandas multi-index, use droplevel:

- "level" kwarg takes an int or string
- "axis" kwarg can be "index" or "columns" — or use 0 or 1, but I prefer the names

Returned data frame has the same number of rows, but index might have duplicates.
October 31, 2026 at 6:37 PM
What does a #Python #Pandas data frame's multi-index look like?

This:

MultiIndex([(0.0, 1),
(1.0, 1),
(2.0, 1),
(3.0, 1),
(4.0, 1),
(5.0, 1),
(6.0, 1)],
names=['passenger_count', 'VendorID'])
October 30, 2026 at 4:30 PM
Using xs on a multi-indexed #Python #Pandas data frame drops the named level — unless you use drop_level=False:

(
pd
.read_parquet('taxi_2025-11.parquet')
.groupby(['passenger_count', 'VendorID'])['total_amount'].mean()
.xs(1, level=1, axis='index', drop_level=False)
)
October 29, 2026 at 8:28 PM
I just got printed copies of my latest book (and my first for O'Reilly), AI-Assisted #Python for Nonprogrammers!
October 29, 2026 at 1:41 PM
Using xs to retrieve from a #Python #Pandas multi-index? "level" takes an int or string:

- int: The index level, with 0 the outermost
- string: The level's name -- but not every level has one

The name is clearer, but sometimes harder to type.
October 28, 2026 at 4:30 PM