pandas DataFrames
pandas is a widely used Python library for tabular data. A DataFrame is a table with labelled columns, and each column is a Series. The first run downloads pandas, so it takes a few seconds.
import pandas as pd
df = pd.DataFrame({
"product": ["laptop", "mouse", "monitor", "keyboard"],
"category": ["computer", "accessory", "computer", "accessory"],
"price": [900, 20, 250, 45],
"units": [3, 10, 4, 7],
})
print(df)
print(df.dtypes)
Columns
Select a column with square brackets. Assigning to a new name creates a new column.
import pandas as pd
df = pd.DataFrame({"price": [900, 20, 250, 45], "units": [3, 10, 4, 7]})
df["revenue"] = df["price"] * df["units"]
print(df)
print(df["revenue"].sum())
print(df.describe())
Filtering rows
Put a condition inside the square brackets to keep the rows where it is true.
import pandas as pd
df = pd.DataFrame({
"product": ["laptop", "mouse", "monitor", "keyboard"],
"price": [900, 20, 250, 45],
})
print(df[df["price"] > 100])
print(df.sort_values("price", ascending=False))
Group and summarise
groupby splits the rows into groups, applies a calculation to each group, and combines the results.
import pandas as pd
df = pd.DataFrame({
"category": ["computer", "accessory", "computer", "accessory"],
"price": [900, 20, 250, 45],
"units": [3, 10, 4, 7],
})
df["revenue"] = df["price"] * df["units"]
print(df.groupby("category")["revenue"].sum())
Try it
Add a row for a "webcam" in the "accessory" category and find the mean price per category with df.groupby("category")["price"].mean().
On your own computer you would normally load data from a file with pd.read_csv("file.csv").