首页 > 解决方案 > 使用其他数据框中的变量将函数应用于 pd 数据框中的所有列

问题描述

我有一系列数据框,有些是静态值,有些是时间序列。

我已经能够添加

我想将值从一个时间序列转换为一个新的时间序列,应用一个从原始时间序列数据帧和保存静态值的数据帧中提取值的函数。

下面是时间序列和静态数据帧的片段。

Time series dataframe (Irradiance)
Datetime             Date        Time    GHI  DIF  flagR   SE    SA  TEMP                                                          
2017-07-01 00:11:00  01.07.2017  00:11    0    0      0  -9.39 -179.97  11.1   
2017-07-01 00:26:00  01.07.2017  00:26    0    0      0  -9.33 -176.47  11.0   
2017-07-01 00:41:00  01.07.2017  00:41    0    0      0  -9.14 -172.98  10.9   
2017-07-01 00:56:00  01.07.2017  00:56    0    0      0  -8.83 -169.51  10.9   
2017-07-01 01:11:00  01.07.2017  01:11    0    0      0  -8.40 -166.04  11.0 

Static dataframe (Properties)
         Bearing (degrees)  Inclination (degrees)
Home ID                                          
151631                 244                     29
151632                 244                     29
151633                 184                     44

我已经编写了一个函数,我想用它来使用这两个值中的值填充一个新的数据框。

def dif(DIF, Inclination, GHI):
    global Albedo
    return DIF * (1 + math.cos(math.radians(Inclination)) / 2) + (GHI * Albedo * (1 - math.cos(math.radians(Inclination)) / 2))

当我尝试做同样的事情时,但在同一个数据帧中,我使用了 Numpy 矢量化函数,所以我认为我可以使用以下代码迭代新数据帧的每一列。

for column in DIF:
    DIF[column] = np.vectorize(dif)(irradiance['DIF'], properties.iloc['Inclination (degrees)'][column], irradiance['GHI'])

相反,这会引发以下错误。

TypeError: cannot do positional indexing on <class 'pandas.core.indexes.numeric.Int64Index'> with these indexers [Inclination (degrees)] of <class 'str'>

我已经检查了 dtypes 的 Inclination(degrees) 值,但它返回为 Int64,而不是 str,所以我不确定为什么会生成此错误。

我显然在这里遗漏了一些关键的东西。有没有更好的替代方法,或者根本没有?任何帮助将非常感激。

标签: pythonpandasnumpy

解决方案


推荐阅读