此代码生成SettingWithCopyWarning,如下所示:
raw_corpus.loc[:,'constructed_recipe']=raw_corpus.loc[:,'trigger_channel_cat'] + " " + raw_corpus.loc[:,'trigger_channel_clean'] + " " + raw_corpus.loc[:,'trigger_name_clean'] + " " + raw_corpus.loc[:,'action_name_clean'] +" " + raw_corpus.loc[:,'action_channel_clean'] +" " + raw_corpus.loc[:,'action_channel_cat']
/Users/dlhoffman/anaconda3/envs/gensim-py35/lib/python3.5/site-packages/pandas/core/indexing.py:537: 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
此代码会产生不同的警告:
raw_corpus['constructed_recipe']=raw_corpus['trigger_channel_cat'] + " " + raw_corpus['trigger_channel_clean'] + " " + raw_corpus['trigger_name_clean'] + " " + raw_corpus['action_name_clean'] +" " + raw_corpus['action_channel_clean'] +" " + raw_corpus['action_channel_cat']
/Users/dlhoffman/anaconda3/envs/gensim-py35/lib/python3.5/site-packages/ipykernel_launcher.py:5: 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
"""
这两段代码都做我想要的,但是错误很烦人,我的理解是这不是一个好错误。我已经阅读了文档和人们的建议,但无法弄清楚我做错了什么。
答案 0 :(得分:2)
这是一个记录良好的错误。看看How to deal with SettingWithCopyWarning in Pandas?。
对于修复,请从
开始raw_corpus = raw_corpus.copy(deep=True)
接下来,获取要汇总的所有列的列表:
cols = ['trigger_channel_cat', 'trigger_channel_clean', ...]
并致电df.agg
:
raw_corpus['constructed_recipe'] = raw_corpus[cols].agg(' '.join, axis=1)