gpt4 book ai didi

python - 具有两个条件的 if 语句

转载 作者:行者123 更新时间:2023-12-01 01:47:20 25 4
gpt4 key购买 nike

我有一个大的 Pandas 表,我想根据风速的分量来操纵风向。目前我有这个:

u=new2_df["U component of wind at 850 Mb over the landfall grid point"].values
v=new2_df["v component of wind at 850 Mb over the landfall grid point"].values
wind_speed=np.sqrt(u**2+v**2)
wind_dir_calc=np.arctan(v/u)
wind_dir=np.degrees(wind_dir_calc)
if np.all(u>0) & np.all(v>0):
wind_dir=360-wind_dir-180
#if np.all(u>0) and np.all(v<0):
#wind_dir=wind_dir+180
#if np.all(u<0) and np.all(v<0):
#wind_dir=180-wind_dir+180
#if np.all(u<0) and np.all(v>0):
#wind_dir=wind_dir

但是,执行此 if 语句不会更改数组 Wind_dir 的值。我想首先使用 u 和 v 计算风向,然后我想根据风向计算过程中 u 和 v 是否为正/负来修改风向。

最佳答案

I want to essentially make my code so that it will change individual elements based on the logic tests I have provided.

如果我理解正确,您可以使用 bool 数组 numpy.where :

wind_dir[np.where((u > 0) & (v > 0))] = 180 - wind_dir

对于所有 u > 0v > 0 的索引,该逻辑会将 wind_dir 中的相应值替换为 180-wind_dir

这是一个演示:

A = np.array([0, -5, 2, 3, -2])
B = np.array([0, 2, -2, 6, 6])
C = np.array([1, 2, 3, 4, 5])

C.flat[np.where((A > 0) & (B > 0))] = 180-C

print(C)
array([ 1, 2, 3, 179, 5])

使用np.ndarray.flat确保索引数组形状通过迭代器与值数组形状对齐。

关于python - 具有两个条件的 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51138775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com