Template request | Bug report | Generate Data Product
Tags: #pandas #pivot #operations #snippet #dataframe
Author: Jeremy Ravenel
Description: This notebook provides an example of how to use the Pandas library to create a pivot table.
import pandas as pd
df = pd.DataFrame(
{
"item": ["apple", "apple", "apple", "apple", "apple"],
"size": ["small", "small", "large", "large", "large"],
"location": ["Walmart", "Aldi", "Walmart", "Aldi", "Aldi"],
"price": [3, 2, 4, 3, 2.5],
}
)
df
pivot = pd.pivot_table(
df, values="price", index=["item", "size"], columns=["location"], aggfunc="mean"
)
pivot