0
1

More than 3 years have passed since last update.

GroupByのメモ

Last updated at Posted at 2020-08-30

DataFrameの作成

qiita.rb
df = pd.DataFrame({'Animal': ['Falcon', 'Falcon',
                              'Parrot', 'Parrot','Parrot'],
                   'Max Speed': [380., 370., 24., 26.,27.],'Annimal_ID': [1, 1,2,2,2]})

df

image.png

Groupby

同じグループに対して処理をしたいときに使用する。

平均

qiita.rb
#FalconやParrotのグループに対して同じ処理をしたいときに使用する
df.groupby(['Animal']).mean()

image.png

サイズを調べる・取得する

qiita.rb
#サイズを調べる
df.groupby(['Animal']).size()

image.png

qiita.rb
#サイズの取得
Falcon_num=df.groupby(['Animal']).size()['Falcon']
print(Falcon_num)

image.png

特定のグループのデータ取得

qiita.rb
#サイズの取得
#特定のグループのデータの取得
df.groupby(['Animal']).get_group('Falcon')

image.png

参考

GropuBy

0
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
0
1