What you want to do with Pandas. -Execute GROUP BY KEY with multiple keys. -Limit the columns to be selected.
import pandas as pd
df = pd.DataFrame({
'city': ['osaka', 'osaka', 'osaka', 'osaka', 'tokyo', 'tokyo', 'tokyo'],
'food': ['apple', 'orange', 'banana', 'banana', 'apple', 'apple', 'banana'],
'price': [100, 200, 250, 300, 150, 200, 400],
'quantity': [1, 2, 3, 4, 5, 6, 7]
})
df[["price","city","food"]].groupby(["city","food"]).sum()
Recommended Posts