Library cheatsheet
pandas and NumPy 3 Cheatsheet
Load, clean, reshape, group and analyze tabular data with pandas, and work with arrays in NumPy.
Loading and inspecting
- Import the libraries — The standard import names.
- Read a CSV file — Load a CSV into a DataFrame, parsing a column as dates.
- Read Excel, JSON or Parquet — pandas reads many formats with similar functions.
- Look at the first rows — Show the first rows. Use tail() for the last ones.
- Check columns and types — Show column names, types, non-null counts and memory use.
- Summarize numeric columns — Show count, mean, spread and quartiles.
- Get the dimensions — Return the number of rows and columns as a tuple.
- Count each distinct value — Count how often each value occurs in a column.
- Create a DataFrame by hand — Build a small DataFrame from a dictionary.
Selecting and filtering
- Select columns — Pick one column as a Series, or several as a DataFrame.
- Filter rows — Keep rows that meet conditions. Combine them with & and |, each in parentheses.
- Filter with a string expression — Write conditions as readable text.
- Select by label — Select rows and columns by label with loc.
- Select by position — Select the first five rows and first two columns by position.
- Match a list of values — Keep rows whose value is in a list.
- Sort rows — Sort by one or more columns.
- Get the top rows — Return the five rows with the largest values.
Cleaning and transforming
- Count missing values — Count the missing values in each column.
- Drop rows with missing values — Remove rows that have a missing value in the given columns.
- Fill missing values — Replace missing values in a column with a default.
- Remove duplicate rows — Keep the first row for each email.
- Rename columns — Rename some columns with a mapping.
- Drop columns — Remove columns you don't need.
- Change a column's type — Convert a column to another type.
- Add a column — Create a column from others. Vectorized operations are much faster than apply.
- Add a column based on a condition — Choose one of two values per row.
- Clean text columns — Apply string methods to a whole column.
- Parse dates — Convert text to datetimes, then pull out parts.
Grouping and combining
- Group and aggregate — Compute several statistics per group with named results.
- Create a pivot table — Summarize values across two dimensions.
- Join two DataFrames — Join on a key, like SQL. how can be inner, left, right or outer.
- Stack DataFrames — Append DataFrames with the same columns.
- Compute a moving average — Average over a sliding window of seven rows.
- Aggregate by time period — Sum values by month. The index must be a datetime.
- Save to a CSV file — Write the DataFrame to a file without the row index.
NumPy
- Create an array — Create an array from a list. Arrays hold one type and operate on all items at once.
- Create a range — Create evenly spaced values, either by step or by count.
- Create filled arrays — Create arrays of zeros, ones or a given value.
- Reshape an array — Change the shape without changing the data. -1 lets NumPy work out the size.
- Generate random numbers — Use the modern generator with a seed so results can be reproduced.
- Compute statistics along an axis — Average each column (axis 0) or each row (axis 1).
- Filter with a boolean mask — Select the items that satisfy a condition.
- Multiply matrices — Use the @ operator for matrix multiplication.
- Sort and find positions — Sort values, or get the indices that would sort them.
- Find distinct values — Get the sorted unique values and how often each occurs.
Related cheatsheets
- Hugging Face cheatsheet — The hf command line, and Transformers and Datasets code for downloading, running, training and sharing models.
- Matplotlib and seaborn 3 cheatsheet — Draw line, scatter, bar and histogram charts with Matplotlib, and statistical charts with seaborn.
- Polars 2 cheatsheet — Fast DataFrames in Python: expressions, filtering, grouping, joins, lazy queries and file formats.
- Prisma ORM cheatsheet — Model your database in a schema, run migrations, and query with the type-safe Prisma Client. Examples use the prisma-client-js generator; check the docs for newer generators.
- PyTorch 2 cheatsheet — Tensors, autograd, models, training loops, saving and speed-ups in PyTorch 2.
- React 19 cheatsheet — Hooks, components and patterns for React 19 in TypeScript or JavaScript.