簡體   English   中英

嘗試獲取不等於列表的元素時的 SettingWithCopyWarning

[英]SettingWithCopyWarning when trying to get elements not equal to list

我正在嘗試刪除不等於列表中元素的 dataframe 中的所有內容,但我收到以下警告:

C:/Users/jalco/PycharmProjects/project/main.py:119: 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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df[sample'] = ''
C:/Users/jalco/PycharmProjects/project/main.py:120: 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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df['sample'] = np.where((df['num'] > 0) &

這是我的代碼導致警告:

if not config_dict['admin']:
    df = df[~df['transtype'].isin(transtype['admin'])]

if 'sample' in config_dict['links']:
    df['sample'] = ''
    df['sample'] = np.where((df['num'] > 0) &
                                    (df['transtype'] == df['coll']),
                                    df['num'], df['sample'])

我的問題是“有沒有更好的方法來刪除我不需要的行,或者我只是手動使警告靜音?”

謝謝

我會在實際創建df時添加.copy() ,因為這似乎是問題的根源,然后您可以嘗試使用.loc[]分配列。 您還可以節省一行代碼,只需使用:

df.loc[:,'sample']  = np.where((df['num'] > 0) &
                                (df['transtype'] == df['coll']),
                                df['num'], ''])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM