Python 试图获取不等于列表的元素时,SettingWithCopyWarning

Python 试图获取不等于列表的元素时,SettingWithCopyWarning,python,pandas,Python,Pandas,我试图删除数据帧中与列表中元素不相等的所有内容,但收到以下警告: 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

我试图删除数据帧中与列表中元素不相等的所有内容,但收到以下警告:

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'], ''])

你能给我们展示一个最小的数据样本,这样我们就可以重现警告了吗?