Anaconda:SettingWithCopy警告:正在尝试在数据帧中的切片副本上设置值



我尝试运行以下代码:

#%% 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 中,但我收到以下错误:

设置与复制警告: 正在尝试在数据帧中的切片副本上设置值

请参阅文档中的注意事项: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。

有人有想法吗?

非常感谢你在阿德瓦克内!

我认为需要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

相关内容

  • 没有找到相关文章

最新更新