我有一个函数relative_humidity(temperature, humidity_index),它有两个变量。
我还有一个DataFrame,其中一列是temperature,另一列是humidity_index,我正在尝试使用此函数创建一个新的湿度列,该列是使用这些行计算得出的。
我曾尝试使用df.apply()函数,但由于我尝试使用多个列,因此该函数对我无效。我还尝试了遍历每一行,并将函数应用于每一行,但这似乎太慢了。感谢您的帮助。
编辑:我的函数看起来像这样:
def relative_humidity_calculator(T, HI):
a = c_6 + c_8*T + c_9*T**2
b = c_3 + c_4*T + c_7*T**2
c = c_1 + c_2*T + c_5*T**2 -HI
solutions = []
#adding both solutions of quadratic to list
if b**2-4*a*c>=0:
solutions.append((-b+np.sqrt(b**2-4*a*c))/(2*a))
solutions.append((-b-np.sqrt(b**2-4*a*c))/(2*a))
#solution is the correct one if it is between 0 and 100
if solutions[0]>0 and solutions[0]<100:
return solutions[0]
else:
return solutions[1]
else:
return print('imaginary roots', T, HI, a, b, c)发布于 2019-11-27 03:11:00
根据您更新的问题,您可以在不包含函数的情况下执行此操作:
# sample data:
c1,c2,c3,c4,c5,c6,c7,c8,c9 = range(9)
np.random.seed(1)
df = pd.DataFrame(np.random.randint(0,100,(10,2)), columns=['T','HI'])
# shorthand for Temp and Humidity-Index
T = df['T']
HI = df['HI']
# series arithmetic operations are allowed
a = c6 + c8*T + c9*T**2
b = c3 + c4*T + c7*T**2
c = c1 + c2*T + c5*T**2 - HI
# discriminant too
deltas = b**2-4*a*c
delta_roots = np.sqrt(b**2 - 4*a*c, where=deltas>0)
# two solutions of quadratic
s0 = (- b + delta_roots)/(2*a)
s1 = (- b - delta_roots)/(2*a)
df['rel_hum'] = np.select(((s0>0) & (s0<100), # condition on first solution
deltas>=0), # quadratic has solutions
(s0, s1), np.nan)输出:
T HI rel_hum
0 37 12 NaN
1 72 9 0.129917
2 75 5 0.028714
3 79 64 -0.629721
4 16 1 NaN
5 76 71 -0.742304
6 6 25 NaN
7 50 20 NaN
8 18 84 NaN
9 11 28 NaNhttps://stackoverflow.com/questions/59054144
复制相似问题