[英]SettingWithCopyWarning when trying to get elements not equal to list
I'm trying to remove everything in a dataframe not equal to elements in a list, but I'm getting the following warning:我正在尝试删除不等于列表中元素的 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) &
Here is my code causing the warning:这是我的代码导致警告:
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'])
My question is "is there a better way to drop the rows I don't need or do I just silence the warning manually?"我的问题是“有没有更好的方法来删除我不需要的行,或者我只是手动使警告静音?”
Thanks谢谢
I would add .copy()
when actually creating df
because that seems to be the root of the problem, and then you can try assigning the column with .loc[]
.我会在实际创建
df
时添加.copy()
,因为这似乎是问题的根源,然后您可以尝试使用.loc[]
分配列。 Also you can save a line of code, by simply using:您还可以节省一行代码,只需使用:
df.loc[:,'sample'] = np.where((df['num'] > 0) &
(df['transtype'] == df['coll']),
df['num'], ''])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.