LoginSignup
0
0

More than 5 years have passed since last update.

異なるデータフレーム間でNan値も含めて値を上書きする

Posted at

NaN値でない値のみ上書きしたい場合は上記質問の通りDataFrame.update()が良い。
NaN値も含めてそっくり上書きしたい場合は、indexとcolumnsを指定すれば良い。


callerA = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3', 'K4', 'K5'],
                        'A': ['A0', 'A1', np.nan, 'A3', 'A4', 'A5'],
                      'B': ['B0', 'B1', 'B2', 'B3', 'B4', 'B5'],
                      })
callerB = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3', 'K4', 'K5'],
                        'A': ['A0', 'A1', 'A2', 'A3', 'A4', np.nan]})
callerA.loc[callerB.index, callerB.columns] = callerB

callerA

image.png

ちゃんとNaN値で更新されている。

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