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 5 years have passed since last update.

pandasのdataframeでreplaceのinplace機能がうまく働かないとき

Last updated at Posted at 2019-10-02

例えばdataframeが'A', 'B'というカラム名を持っているとします。
'A' に 'tokyo'と書いてある行で'B'列で1という値を持っているところを2に置換したいとします。

最初は
df[df['A'] == 'tokyo']['B'].replace(1, 2, inplace=True)と書いていたんですが、A value is trying to be set on a copy of a slice from a DataFrame`
のようなエラーメッセージが出ました。

最終的に、
df.loc[df.A == 'tokyo', 'B'] = df.loc[df.A == 'tokyo', 'B'].replace(1.0, 0.0)
のようにしたら元のdataframeにも反映されてくれました。

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?