使用.loc[row_indexer,col_indexer]=value不允许更改要修改的值



我试图更改数据帧的一些单元格,但我仍然得到了SettingWithCopyWarning,而没有替换我想要的内容。生效日期:

df.loc[df["subject"]!="subject1"]["Activity"] = np.nan

我得到了:

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
"""Entry point for launching an IPython kernel.

当我试图将其可视化时,一切都没有改变:

161280     0
161281     0
161282     0
161283     0
161284     0
..
1215740    0
1215741    0
1215742    0
1215743    0
1215744    0
Name: Activity, Length: 1054465, dtype: int64

如下修改,要设置的值应该在loc中指定。请参阅pd.dataframe.loc

df.loc[df["subject"]!="subject1", "Activity"] = np.nan

最新更新