将定义应用于数据框中单列中的 3 个选定部分和 8 个固定值

applying a definition on 3 selected parts in single column in a dataframe and 8 fixed values

我正在尝试使用我在一个文本文件中的恒定坐标值和变化的风速值来插入值。使用此定义:

def bary_interpol (inter_cor_x1, inter_cor_x2, inter_cor_x3, inter_cor_y1,
                   inter_cor_y2, inter_cor_y3, inter_cor_px, inteR_cor_py,
                   inter_wind_v1, inter_wind_v2, inter_wind_v3):
    W_v1 = ((inter_cor_y2-inter_cor_y3)*(inter_cor_px-inter_cor_x3)+
            (inter_cor_x3-inter_cor_x2)*(inter_cor_py-inter_cor_y3))/((inter_cor_y2-inter_cor_y3)*(inter_cor_x1-inter_cor_x3)+
            (inter_cor_x3-inter_cor_x2)*(inter_cor_y1-inter_cor_y3))
    W_v2 = ((inter_cor_y3-inter_cor_y1)*(inter_cor_px-inter_cor_x3)+
            (inter_cor_x1-inter_cor_x3)*(inter_cor_py-inter_cor_y3))/((inter_cor_y2-inter_cor_y3)*(inter_cor_x1-inter_cor_x3)+
            (inter_cor_x3-inter_cor_x2)*(inter_cor_y1-inter_cor_y3))
    W_v3 = 1 - W_v1 - W_v2
    inter_wind_pv = (W_v1 * inter_wind_v1) + (W_v2 * inter_wind_v2) +  (W_v3 * inter_wind_v3)
    return inter_wind_pv

这个定义现在对我有用,但前提是所有变量都是常量。但是 inter_wind_v1、inter_wind_v2 和 inter_wind_v3 需要从文本文件中添加。

(FF)列中的值我已经通过与FF在同一行的标识号找到了。 STN_V1其实代表235,我用的是这个方法:

inter_wind_v1 = wind[wind['STN'] == STN_v1]
print(inter_wind_v1)

inter_wind_v2 = wind[wind['STN'] == STN_v2]
print(inter_wind_v2)

inter_wind_v3 = wind[wind['STN'] == STN_v3]
print(inter_wind_v3)

这将打印我需要的选定数据。

          STN        DT     FF
10464  235  20210101     20
10465  235  20210101     30
10466  235  20210101     20
10467  235  20210101     20
10468  235  20210101     20
   ...       ...    ...
20923  235  20220312     40
20924  235  20220312     50
20925  235  20220312     50
20926  235  20220312     50
20927  235  20220312     60

[10464 rows x 3 columns]
       STN        DT     FF
20928  242  20210101     80
20929  242  20210101     60
................................ etc.

但我不知道如何使用我为插值所做的定义制作新列表或数据框。

所以我试图创建一个内插的新列 FF,将定义中引用为 inter_wind_v 的三个 FF 列组合起来。

我希望有人能帮助我。

我相信这可能是解决您的问题的方法:

import pandas as pd
def bary_interpol (inter_cor_x1, inter_cor_x2, inter_cor_x3, inter_cor_y1,
                   inter_cor_y2, inter_cor_y3, inter_cor_px, inter_cor_py,
                   inter_wind_v1, inter_wind_v2, inter_wind_v3):
    W_v1 = ((inter_cor_y2-inter_cor_y3)*(inter_cor_px-inter_cor_x3)+
            (inter_cor_x3-inter_cor_x2)*(inter_cor_py-inter_cor_y3))/((inter_cor_y2-inter_cor_y3)*(inter_cor_x1-inter_cor_x3)+
            (inter_cor_x3-inter_cor_x2)*(inter_cor_y1-inter_cor_y3))
    W_v2 = ((inter_cor_y3-inter_cor_y1)*(inter_cor_px-inter_cor_x3)+
            (inter_cor_x1-inter_cor_x3)*(inter_cor_py-inter_cor_y3))/((inter_cor_y2-inter_cor_y3)*(inter_cor_x1-inter_cor_x3)+
            (inter_cor_x3-inter_cor_x2)*(inter_cor_y1-inter_cor_y3))
    W_v3 = 1 - W_v1 - W_v2
    inter_wind_pv = (W_v1 * inter_wind_v1) + (W_v2 * inter_wind_v2) +  (W_v3 * inter_wind_v3)
    return inter_wind_pv

wind_records = [
    {'STN':235, 'DT':20210101, 'FF':20},
    {'STN':235, 'DT':20210101, 'FF':30},
    {'STN':235, 'DT':20210101, 'FF':20},
    {'STN':236, 'DT':20210101, 'FF':20},
    {'STN':236, 'DT':20210101, 'FF':20},
    {'STN':236, 'DT':20210101, 'FF':40},
    {'STN':237, 'DT':20210101, 'FF':50},
    {'STN':237, 'DT':20210101, 'FF':50},
    {'STN':237, 'DT':20210101, 'FF':50}
]
wind = pd.DataFrame(wind_records)

STN_v1 = 235
inter_wind_v1 = wind[wind['STN'] == STN_v1].reset_index()
print(f"inter_wind_v1:\n{inter_wind_v1}")

STN_v2 = 236
inter_wind_v2 = wind[wind['STN'] == STN_v2].reset_index()
print(f"inter_wind_v2:\n{inter_wind_v2}")

STN_v3 = 237
inter_wind_v3 = wind[wind['STN'] == STN_v3].reset_index()
print(f"inter_wind_v3:\n{inter_wind_v3}")

interp = pd.DataFrame(inter_wind_v1['FF']).rename(columns = {'FF':'inter_wind_v1'})
interp['inter_wind_v2'] = inter_wind_v2['FF']
interp['inter_wind_v3'] = inter_wind_v3['FF']
print(interp)

inter_cor_x1, inter_cor_x2, inter_cor_x3 = 0.1, 0.01, -0.1
inter_cor_y1, inter_cor_y2, inter_cor_y3 = 0.2, 0.05, -0.1
inter_cor_px, inter_cor_py = 0.22, -0.22
def doInterp(inter_wind_v1, inter_wind_v2, inter_wind_v3):
    return bary_interpol (inter_cor_x1, inter_cor_x2, inter_cor_x3, inter_cor_y1,
                   inter_cor_y2, inter_cor_y3, inter_cor_px, inter_cor_py,
                   inter_wind_v1, inter_wind_v2, inter_wind_v3)
interp['inter_wind_pv'] = interp.apply(lambda x: doInterp(x['inter_wind_v1'], x['inter_wind_v2'], x['inter_wind_v3']), axis=1)
print(interp)

输出:

inter_wind_v1:
   index  STN        DT  FF
0      0  235  20210101  20
1      1  235  20210101  30
2      2  235  20210101  20
inter_wind_v2:
   index  STN        DT  FF
0      3  236  20210101  20
1      4  236  20210101  20
2      5  236  20210101  40
inter_wind_v3:
   index  STN        DT  FF
0      6  237  20210101  50
1      7  237  20210101  50
2      8  237  20210101  50
   inter_wind_v1  inter_wind_v2  inter_wind_v3
0             20             20             50
1             30             20             50
2             20             40             50
   inter_wind_v1  inter_wind_v2  inter_wind_v3  inter_wind_pv
0             20             20             50         -538.0
1             30             20             50         -742.0
2             20             40             50          262.0

我做了以下假设:

  • 使用了 STN_v1STN_v2STN_v3 的示例值以及数据帧内容
  • 假设 inter_wind_v1inter_wind_v2inter_wind_v3
  • 的项目数相同
  • 假设 bary_interpol() 函数的其他参数是标量(常量),并且您想使用除 inter_wind_v1、[=16= 之外的相同参数值重复调用该函数] 和 inter_wind_v3