[PYTHON] Cross tabulation using Pandas

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]
})

image.png

df[["price","city","food"]].groupby(["city","food"]).sum()

image.png

Recommended Posts

Cross tabulation using Pandas
Draw multiple graphs using Pandas
Data analysis using python pandas
[Python] Loading csv files using pandas
Data visualization method using matplotlib (+ pandas) (5)
Analyze JMeter jtl files using pandas
Pandas
Precautions when using codecs and pandas
Data visualization method using matplotlib (+ pandas) (3)
100 Language Processing Knock-31 (using pandas): Verb
Cases using pandas plot, cases using (pure) matplotlib plot
Data visualization method using matplotlib (+ pandas) (4)
100 Language Processing Knock-38 (using pandas): Histogram
100 Language Processing Knock-33 (using pandas): Sahen noun
Precautions when using for statements in pandas
100 Language Processing Knock-35 (using pandas): Noun concatenation
100 Language Processing Knock-39 (using pandas): Zipf's Law
Create a dataframe from excel using pandas
I analyzed cowrie (honeypot) using python pandas
100 Language Processing Knock-34 (using pandas): "A B"