问题解决:SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

与世无争的帅哥 提交于 2020-11-15 20:42:00

 在函数中修改:

def countGroupWord(df_sentence):
    stop_words = list(stopwords.words('english'))
    ....
    df_sentence['classId']=df_sentence['classId']
df_sentence['classId']=df_sentence['classId']会报错

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
df_sentence['classId']=df_sentence['abstract']

这个报警主要是说,你当前对B的操作可能会改变另一个DataFrame A

我新建一个对象再赋值就不会报错了,如果不在函数里面可以直接写df_sentence['classId']=df_sentence['abstract'],但作为函数参数修改它是不安全的(我猜是)

def countGroupWord(df_sentence):
    stop_words = list(stopwords.words('english'))
   
    df_title = df_sentence['title'] +  df_sentence['abstract']
    df_title['classId']=df_sentence['classId']

在main函数中执行的话就没问题

df_data=handlData(path_Data)
df_data['abstract']=df_data['title'];

执行通过。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!