LoginSignup
1
3

More than 5 years have passed since last update.

pandasのデータ置換様々

Posted at

pandasによるデータ置換

まえがき

pandasにはデータの置換1つをとっても様々なやり方があり、非常に混乱させられる。
このことは私を含む独学の初学者の学習を遅らせる原因となっていると思うので、簡単にまとめてみる。
1~9の連続数が収められた3×3行列の2列目の4以上の値を欠損値に置換する。

インデックス参照による代入

df = pd.DataFrame(np.arange(9).reshape(3, 3), index=['row1', 'row2', 'row3'], columns=['col1', 'col2', 'col3'])

df.loc[sample['col2'] >= 4, 'col2'] = np.nan

df.where

df.col2.where(df.col2 < 4, np.nan, inplace=True)

df.query

df.query('df.col2 >= 4', inplace=True)
1
3
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
1
3