0
0

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 1 year has passed since last update.

二つのDataFrameを比較して代入を行う方法(pd.mergeの使い方)

Last updated at Posted at 2022-01-27

毎回書き方を忘れるので、以下、備忘録です。

DataFrame① df

time_index data
0 2001 10
1 2002 20
2 2003 30
3 2004 40

|
|3 |2011|40 |
|
|19|2020 |50 |

DataFrame ② dx

time_index data
0 2010 1
1 2011 20
2 2012 3
3 2013 4

|
|
|10|2020 |4|

このDataFrame①とDataFrame②をmergeする。

df = pd.merge(df, dx, on='time_index', how='left')

とすることで、

time_index data_x data_y
0 2001 10 nan
1 2002 20 nan
2 2003 30 nan
3 2004 40 nan

|
|10 |2011|40 |20|
|
|19|2020 |50 |4 |

となる。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?