私はDataFrameを持っています。統計条件付きテストを行うために、ブール値の列( 'mar')に基づいて2つに分割しました。以下のように、2つの表の間のカウントの比率を使用して、他の列の組み合わせごとに 'mar'列に真の値の割合を表す列を追加します。このパンダの警告はどこから来たのですか?
>>> df_nomar
alc cig mar cnt
1 1 1 0 538
3 1 0 0 456
5 0 1 0 43
7 0 0 0 279
>>> df_mar
alc cig mar cnt
0 1 1 1 911
2 1 0 1 44
4 0 1 1 3
6 0 0 1 2
>>> df_mar.loc[:, 'prop'] = np.array(df_mar['cnt'])/(np.array(df_mar['cnt']) + np.array(df_nomar['cnt']))
/usr/local/lib/python3.5/dist-packages/pandas/core/indexing.py:296: 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: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self.obj[key] = _infer_fill_value(value)
/usr/local/lib/python3.5/dist-packages/pandas/core/indexing.py:476: 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: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self.obj[item] = s
>>> df_mar
alc cig mar cnt prop
0 1 1 1 911 0.628709
2 1 0 1 44 0.088000
4 0 1 1 3 0.065217
6 0 0 1 2 0.007117
私は警告を調査するためにsuggested pageに行ってきました。新しい列を割り当てるときは、提案通りにdf_mar.loc[:, 'prop'] = ...
の形式を使用しています。
なぜ私はまだこの警告を受けていますか?
関連:[chained-assignment](https://stackoverflow.com/questions/tagged/chained-assignment?sort=votes&pageSize=30)タグ –
https://www.dataquestを理解するためにこのブログを読んでください。 io/blog/settingwithcopywarning/ – Tanu