pandas and NumPy 3 cheatsheet

pandas and NumPy 3: Loading and inspecting

9 common pandas and NumPy 3 tasks.

Import the libraries

The standard import names.

import pandas as pd
import numpy as np

Read a CSV file

Load a CSV into a DataFrame, parsing a column as dates.

df = pd.read_csv("data.csv", parse_dates=["created_at"])

Read Excel, JSON or Parquet

pandas reads many formats with similar functions.

df = pd.read_excel("data.xlsx")
df = pd.read_json("data.json")
df = pd.read_parquet("data.parquet")

Look at the first rows

Show the first rows. Use tail() for the last ones.

df.head()

Check columns and types

Show column names, types, non-null counts and memory use.

df.info()

Summarize numeric columns

Show count, mean, spread and quartiles.

df.describe()

Get the dimensions

Return the number of rows and columns as a tuple.

df.shape

Count each distinct value

Count how often each value occurs in a column.

df["country"].value_counts()

Create a DataFrame by hand

Build a small DataFrame from a dictionary.

df = pd.DataFrame({"name": ["Ada", "Grace"], "age": [36, 45]})

Was this page helpful?