Last updated
Pandas DataFrames
Definition: A DataFrame is a 2D table of rows and columns. Each column is a Series, and all columns share the same row index. This is the object you will use most in Pandas.
Example — build and inspect
import pandas as pd
df = pd.DataFrame({
"name": ["Sam", "Alex", "Jo"],
"city": ["London", "Paris", "Rome"],
"age": [25, 30, 22]
})
print(df)
print("Shape:", df.shape) # (3 rows, 3 cols)
print("Columns:", df.columns.tolist())
Useful attributes
df.shape— (rows, columns)df.columns— the column namesdf.dtypes— the type of each columndf.index— the row labels
Each column is a Series
import pandas as pd
df = pd.DataFrame({"name": ["Sam", "Alex"], "age": [25, 30]})
print(type(df["age"])) # a Series
💡 Tip: a DataFrame is just a collection of columns (Series) that line up by row — keep that picture in mind.
Try it Yourself
Output
Ad · responsive