0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DataFrameの結合

Last updated at Posted at 2019-07-03

concat

単純結合する場合

#縦方向
pd.concat([df1, df2])

#横方向
pd.concat([df1, df2], axis=1)

共通項目のみ結合する場合

pd.concat([df1, df4], axis=1, join='inner')

merge

表df1 と 表df2 を 条件値の値一致で結合する場合

#結合条件:id列の一致 (左表優先)
df3 = pd.merge( df1 ,  df2 ,  on='id',how='left')

#結合条件:id列とkye列の一致
df3 = pd.merge( df1 ,  df2 ,  on=['id','key'])

#join
indexをキーに結合する

df1.join(df2, how='inner')#共通項のみ結合
df1.join(df2, how='right')#右表(df2)優先結合
df1.join(df2, how='outer')#全行結合 値がない場合はnan値

#append

行番号=nameのある行(Series)を追加する

s1 = pd.Series(['X0', 'X1', 'X2', 'X3'],
               index=['A', 'B', 'C', 'D'], name=10)
df1.append(s1)

#その他(表関連)

drop_duplicates

idが重複する行を削除

df1.drop_duplicates(subset='id')
0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?