Anaconda:SettingWithCopyWarning:尝试在DataFrame

时间:2018-06-06 11:45:10

标签: python pandas anaconda

我尝试运行以下代码:

#%% add offsests and remove not qualified data 
df=df[df['surfHeightr1_qual']==0] # eliminate values where the quality flag == 1

#add offset to SIN SurfType 1 and 3 --> this is needed when data is baseline C 
if df['surfHeightr1'][(df['OPERATION_MODE']=='SIR_SIN_L2') & (df['surfType']==1) & (df['BaselineID']=='C')].any() == True:
df['surfHeightr1'][(df['OPERATION_MODE']=='SIR_SIN_L2') & (df['surfType']==1)& (df['BaselineID']=='C')]=df['surfHeightr1'][(df['OPERATION_MODE']=='SIR_SIN_L2') & (df['surfType']==1)& (df['BaselineID']=='C')]+59.959
if df['surfHeightr1'][(df['OPERATION_MODE']=='SIR_SIN_L2') & (df['surfType']==3)& (df['BaselineID']=='C')].any() == True:   
df['surfHeightr1'][(df['OPERATION_MODE']=='SIR_SIN_L2') & (df['surfType']==3)& (df['BaselineID']=='C')]=df['surfHeightr1'][(df['OPERATION_MODE']=='SIR_SIN_L2') & (df['surfType']==3)& (df['BaselineID']=='C')]+59.959
在Python中

,但我得到了以下错误:

SettingWithCopyWarning: 尝试在DataFrame

的切片副本上设置值

请参阅文档中的警告:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

df['surfHeightr1'][(df['OPERATION_MODE']=='SIR_SIN_L2') & (df['surfType']==3)& (df['BaselineID']=='C')]=df['surfHeightr1'][(df['OPERATION_MODE']=='SIR_SIN_L2') & (df['surfType']==3)& (df['BaselineID']=='C')]+59.959

有趣的是,错误只发生在第二个if,所以我不明白错误发生的原因。此外,df的所有后续用法都会被代码忽略。

我在Ubuntui 16.04中使用Python 3.6.3和Anaconda。

有人有想法吗?

非常感谢advacne!

1 个答案:

答案 0 :(得分:0)

我认为需要loc以避免SettingWithCopyWarning,代码应该由+=简化:

mask = (df['OPERATION_MODE']=='SIR_SIN_L2') & (df['surfType']==3) & (df['BaselineID']=='C')
df.loc[mask, 'surfHeightr1'] +=59.959

编辑:

对于完全解决方案的使用numpy.select并且为了更好的可读性,每个条件都分配给单独的变量:

df = pd.DataFrame({'BaselineID':list('CCCCCD'),
                   'surfHeightr1_qual':[0,0,0,0,0,1],
                   'OPERATION_MODE':['SIR_SIN_L2'] * 6,
                   'surfType':[1,3,1,5,1,0],
                   'surfHeightr1':[5,3,6,9,2,4],
                   'F':list('aaabbb')})

print (df)
  BaselineID  surfHeightr1_qual OPERATION_MODE  surfType  surfHeightr1  F
0          C                  0     SIR_SIN_L2         1             5  a
1          C                  0     SIR_SIN_L2         3             3  a
2          C                  0     SIR_SIN_L2         1             6  a
3          C                  0     SIR_SIN_L2         5             9  b
4          C                  0     SIR_SIN_L2         1             2  b
5          D                  1     SIR_SIN_L2         0             4  b

df=df[df['surfHeightr1_qual']==0].copy() # eliminate values where the quality flag == 1

#add offset to SIN SurfType 1 and 3 --> this is needed when data is baseline C 
m1 = df['OPERATION_MODE']=='SIR_SIN_L2'
m2 = df['BaselineID']=='C'
m3 = df['surfType']==1
m4 = df['surfType']==3

m123 = m1 & m2 & m3
m124 = m1 & m2 & m4

df['surfHeightr1'] += np.select([m123, m124], [59.959, 59.959], default=0)
print (df)
  BaselineID  surfHeightr1_qual OPERATION_MODE  surfType  surfHeightr1  F
0          C                  0     SIR_SIN_L2         1        64.959  a
1          C                  0     SIR_SIN_L2         3        62.959  a
2          C                  0     SIR_SIN_L2         1        65.959  a
3          C                  0     SIR_SIN_L2         5         9.000  b
4          C                  0     SIR_SIN_L2         1        61.959  b