問題解決:SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame


 在函數中修改:

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'];

執行通過。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM