Python正在删除带有CopyWarning的设置

Python正在删除带有CopyWarning的设置,python,pandas,pattern-matching,Python,Pandas,Pattern Matching,因此,我使用 df=data[['ID','Matrix','Name','Country', 'Units']] df['Value']='' 我用这样的代码填充它,在df.Matrix中找到包含'Good','Bad'值的字符串,并在sch[I]中用值填充它们: df.loc[df.Matrix.str.contains('Good'),'Value'] = sch[2] df.loc[df.Matrix.str.contains('Bad'),'Value'] = sch[6] df.l

因此,我使用

df=data[['ID','Matrix','Name','Country', 'Units']]
df['Value']=''
我用这样的代码填充它,在
df.Matrix
中找到包含'Good','Bad'值的字符串,并在
sch[I]
中用值填充它们:

df.loc[df.Matrix.str.contains('Good'),'Value'] = sch[2]
df.loc[df.Matrix.str.contains('Bad'),'Value'] = sch[6]
df.loc[df.Matrix.str.contains('Excellent'),'Value'] = sch[8]
我一直在犯一些错误,比如这两个不同的错误:

C:\Python33\lib\site-packages\pandas\core\strings.py:184: UserWarning: This pattern has match groups. To actually get the groups, use str.extract.
  " groups, use str.extract.", UserWarning)

C:\Users\0\Desktop\python\Sorter.py:57: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
  df.loc[df.Matrix.str.contains('Bad'),'Value'] = sch[6]
到目前为止,我正在使用

pd.options.mode.chained_assignment = None
如果我不抑制错误消息,我将得到大约20条。是否有其他格式可以更改数据以避免收到错误消息


我使用的是python 3和pandas 0.131,如果它有帮助的话,这里有一个很好的解释,说明为什么会打开此警告:

你确定这就是你的全部代码吗?请展示你正在做的一切

In [13]: df = DataFrame(index=range(5))

In [14]: df['Value'] = ''

In [15]: df.loc[[1,4],'Value'] = 'bad'

In [16]: df.loc[[0,3],'Value'] = 'good'

In [17]: df
Out[17]: 
  Value
0  good
1   bad
2      
3  good
4   bad

[5 rows x 1 columns]
第二个例子

In [1]: df = DataFrame(index=range(5))

In [2]: df['Value'] = ''

In [3]: df2 = DataFrame(dict(A=['foo','foo','bar','bar','bah']))

In [4]: df
Out[4]: 
  Value
0      
1      
2      
3      
4      

[5 rows x 1 columns]

In [5]: df2
Out[5]: 
     A
0  foo
1  foo
2  bar
3  bar
4  bah

[5 rows x 1 columns]

In [6]: df.loc[df2.A.str.contains('foo'),'Value'] = 'good'

In [7]: df.loc[df2.A.str.contains('bar'),'Value'] = 'bad'

In [8]: df
Out[8]: 
  Value
0  good
1  good
2   bad
3   bad
4      

[5 rows x 1 columns]

您的代码假定您知道我的代码找到的每个匹配值的位置并标记它们。这不是全部代码,因为文件太大。在我上面的例子中,我不知道值在
df.Matrix
中的什么位置,所以
str.contains('Good')
检查
bool值,然后用sch[I]中的任何内容标记它,检查bool值的部分会导致错误。您的代码没有错误,因为您分配了位置和输入值。这是不一样的,因为如果我给你一个混合数据的文件,并告诉你提取'好',你的程序将无法工作,我添加了一个像你的例子;这是一样的,我知道,因为我写的代码:)奇怪,我不知道为什么我会得到错误。他们似乎是一样的。是因为我的所有数据都在同一数据帧上编辑吗?最好的方法是创建序列,然后直接分配它,例如,
df['Value']=s
,而不是创建空的和覆盖的值。只需创建您需要的系列;pandas将对齐它(用nan填充剩余值)