pythonで以下のようにしたところSettingWithCopyWarningという警告が出ました。
プログラムはそのまま進行しましたが気持ちが悪いので解決したいです。
python
1print(df) 2df['C'] = df['A']/df['B'] 3print(df)
output
1 A B 25 3 2 36 11 5 414 2 1 515 9 3 618 5 4 7... ... ... 841 25 10 944 8 2 1047 8 8 1148 17 4 1251 6 3 13 14[20 rows x 2 columns] 15hogehoge.py:84: SettingWithCopyWarning: 16A value is trying to be set on a copy of a slice from a DataFrame. 17Try using .loc[row_indexer,col_indexer] = value instead 18 19See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy 20 df['C'] = df['A']/df['B'] 21 A B C 225 3 2 1.5 236 11 5 2.2 2414 2 1 2.0 2515 9 3 3.0 2618 5 4 1.25 27... ... ... ... 2841 25 10 2.5 2944 8 2 4.0 3047 8 8 1.0 3148 17 4 4.25 3251 6 3 2.0 33 34[20 rows x 3 columns]
.locを使えと書かれているので試みましたが駄目でした。
python
1print(df) 2df.loc[:,'C'] = df.loc[:,'A']/df.loc[:,'B'] 3print(df)
output
1 A B 25 3 2 36 11 5 414 2 1 515 9 3 618 5 4 7... ... ... 841 25 10 944 8 2 1047 8 8 1148 17 4 1251 6 3 13 14[20 rows x 2 columns] 15/home/hoge/anaconda3/lib/python3.7/site-packages/pandas/core/indexing.py:1596: SettingWithCopyWarning: 16A value is trying to be set on a copy of a slice from a DataFrame. 17Try using .loc[row_indexer,col_indexer] = value instead 18 19See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy 20 self.obj[key] = _infer_fill_value(value) 21/home/hoge/anaconda3/lib/python3.7/site-packages/pandas/core/indexing.py:1743: SettingWithCopyWarning: 22A value is trying to be set on a copy of a slice from a DataFrame. 23Try using .loc[row_indexer,col_indexer] = value instead 24 25See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy 26 isetter(ilocs[0], value) 27 A B C 285 3 2 1.5 296 11 5 2.2 3014 2 1 2.0 3115 9 3 3.0 3218 5 4 1.25 33... ... ... ... 3441 25 10 2.5 3544 8 2 4.0 3647 8 8 1.0 3748 17 4 4.25 3851 6 3 2.0 39 40[20 rows x 3 columns]
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/05/13 02:28