python 替换并筛选值错误:'值错误:用序列设置数组元素,

rslzwgfq  于 2023-01-24  发布在  Python
关注(0)|答案(1)|浏览(199)

我有这个df:

VELWIND
0
1
34
54
2
3
25

我尝试做的是过滤0到30之间的值,如果df >= 30,则写nandf.typefloat64
我有这个代码:

df['VELWIND_filt']=df.loc[(df['VELWIND'] > 0) & (df['VELWIND'] < 30) & (df['VELWIND'] >=30, np.nan)]

但我有个错误
ValueError: setting an array element with a sequence.
我测试它给出了一个错误,具体是这个部分:

(df['VELWIND'] >=30, np.nan)

编辑解决:
我解决这个问题使用这行:
df['VELWIND_filt'] = np.where((df['VELWIND'] < 0) | (df['VELWIND'] >= 30), np.nan, df['VELWIND'])

y1aodyip

y1aodyip1#

这就是你想要的吗?

df[(df['VELWIND'] < 0) | (df['VELWIND'] >= 30)] = np.nan
print(df)

如果您想将其添加为新列,请先创建新列,然后进行更改:

df['VELWIND_FILT'] = df['VELWIND']
df[(df['VELWIND_FILT'] < 0) | (df['VELWIND_FILT'] >= 30)] = np.nan
print(df)

相关问题