[英]Replace and filter value Error: 'ValueError: setting an array element with a sequence.'
I've this df:我有这个 df:
VELWIND
0
1
34
54
2
3
25
What I'm trying to do is filter values between 0 and 30, and write nan
if df >= 30
.我想做的是过滤 0 到 30 之间的值,如果df >= 30
则写nan
。 The df.type
is float64
df.type
是float64
I have this code:我有这段代码:
df['VELWIND_filt']=df.loc[(df['VELWIND'] > 0) & (df['VELWIND'] < 30) & (df['VELWIND'] >=30, np.nan)]
but I have this error:但我有这个错误:
ValueError: setting an array element with a sequence.
I was testing that it gave an error, and it is this part specifically:我正在测试它是否出错,具体是这部分:
(df['VELWIND'] >=30, np.nan)
EDIT SOLVED:编辑解决:
I solved this problem used this line:我用这条线解决了这个问题:
df['VELWIND_filt'] = np.where((df['VELWIND'] < 0) | (df['VELWIND'] >= 30), np.nan, df['VELWIND'])
Is this what you want?这是你想要的吗?
df[(df['VELWIND'] < 0) | (df['VELWIND'] >= 30)] = np.nan
print(df)
If you want to add it as new column, create the new column first THEN make the changes:如果要将其添加为新列,请先创建新列然后进行更改:
df['VELWIND_FILT'] = df['VELWIND']
df[(df['VELWIND_FILT'] < 0) | (df['VELWIND_FILT'] >= 30)] = np.nan
print(df)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.