LoginSignup
2
1

More than 3 years have passed since last update.

Pandasを用いたクロス集計

Last updated at Posted at 2020-01-08

Pandasを用いて実行したいこと。
・GROUPBY KEYを複数キーで実行。
・SELECT対象のカラムを限定する。

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

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1