如何应用将多列用作熊猫输入的函数?

时间:2019-11-26 15:24:53

标签: pandas

我有一个函数relative_humidity(temperature, humidity_index),它带有两个变量。

我也有一个DataFrame,其中一列是温度,另一列是湿度指数,我正尝试使用此功能来创建一个新的湿度列,并使用这些行计算得出。

我尝试使用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)

1 个答案:

答案 0 :(得分:1)

根据您更新的问题,您可以在不包含以下功能的情况下执行此操作:

# 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       NaN