我正在尝试将 DataFrame london
中的列 'let'
(这是另一个 no_eco
的副本)替换为以下行:仅包含 contains()
方法中的字符串。代码如下:
london = no_eco
london.loc[:,'let'] = london.loc[:,'let'].str.contains('E' or 'D' or 'F' or 'G' or 'H' or 'I' or 'J')
london.loc[:,'let'] = london.loc[:,'let'][london.loc[:,'let']]
london = london.dropna(subset = ['let'])
print(london)
代码有效,我已经删除了字符串不满足的行,但我收到以下警告:
C:\Users\gerardchurch\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexing.py:543: 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
当我查看文档时,我仍然无法理解我做错了什么。
继续使用变量london
可以吗?否则我将来会遇到问题吗?
谢谢。
最佳答案
您的代码存在几个问题:
london = no_eco
不会将副本分配给london
。明确一点:london = no_eco.copy()
。-
pd.Series.str.contains
默认支持正则表达式,因此请使用str.contains('E|D|F|G|H|I|J|')
。 - 你的逻辑很困惑。首先将
object
dtype 系列替换为 bool 系列,然后为其分配一个由其自身索引的子集,然后使用dropna
,它专为 null 值。
相反,只需构造一个 bool 系列并使用 pd.DataFrame.loc
使用 bool 索引:
london = no_eco.copy()
london = london.loc[london['let'].str.contains('E|D|F|G|H|I|J|')]
对于这种特殊情况,您可以直接使用 pd.DataFrame.__getitem__
(df[]
语法):
london = no_eco.copy()
london = london[london['let'].str.contains('E|D|F|G|H|I|J|')]
关于python - 接收SettingWithCopyWarning。继续安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53191089/