【问题标题】:Dataframe Interpolation based on table values基于表值的数据帧插值
【发布时间】:2020-11-10 00:36:29
【问题描述】:

我有一个具有不同纬度值的数据集,这些纬度的范围在 0 到 20 之间,月份在 1-12 之间 如何在我的数据集中计算一个新行,该行的每个纬度和月份的结果为 N? 由于纬度在我的数据集中不是整数值,因此需要进行插值

输入数据集

    LAT YEAR    MONTH
0   11  2000    1
1   9   2000    2
2   11  2000    3
3   10  2000    4
4   17  2000    5
5   6   2000    6
6   18  2000    7
7   11  2000    8
8   17  2000    9
9   12  2000    10
10  19  2000    11
11  8   2000    12
12  14  2001    1
13  13  2001    2
14  14  2001    3
15  12  2001    4
16  12  2001    5
17  5   2001    6
18  18  2001    7
19  13  2001    8
20  7   2001    9
21  18  2001    10
22  12  2001    11
23  10  2001    12
24  14  2002    1
25  14  2002    2
26  20  2002    3
27  20  2002    4
28  9   2002    5
29  15  2002    6
30  15  2002    7
31  5   2002    8
32  7   2002    9
33  5   2002    10
34  6   2002    11
35  7   2002    12

按纬度按月计算的N个值

      1      2      3      4       5       6       7       8       9       10      11      12
lat                                             
0   1.04    0.94    1.04    1.01    1.04    1.01    1.04    1.04    1.01    1.04    1.01    1.04
10  1.00    0.91    1.03    1.03    1.08    1.06    1.08    1.07    1.02    1.02    0.98    0.99
15  0.97    0.91    1.03    1.04    1.11    1.08    1.12    1.08    1.02    1.01    0.95    0.97
20  0.95    0.90    1.03    1.65    1.13    1.11    1.14    1.12    1.02    1.00    0.93    0.94

N值表的代码是:

data2 = {"lat":[0,10,15,20],"1":[1.04,1,0.97,0.95],"2":[0.94,0.91,0.91,0.9],"3":[1.04,1.03,1.03,1.03],
"4":[1.01,1.03,1.04,1.65],"5":[1.04,1.08,1.11,1.13],"6":[1.01,1.06,1.08,1.11],"7":[1.04,1.08,1.12,1.14],"8":[1.04,1.07,1.08,1.12],
"9":[1.01,1.02,1.02,1.02],"10":[1.04,1.02,1.01,1],"11":[1.01,0.98,0.95,0.93],"12":[1.04,0.99,0.97,0.94]}
df2 = pd.DataFrame(data2)

例如,如果纬度为 20,月份为 3,则 N 列中的结果必须为 1.03,如果现在纬度为 11 且月份为 1,则 N 列中的结果必须为 0.97 或多或少

【问题讨论】:

    标签: python arrays pandas dataframe


    【解决方案1】:

    在您的情况下,使用 pd.interpolate() 方法会非常方便:

    import pandas as pd
    
    data2 = {"lat":[0,10,15,20],"1":[1.04,1,0.97,0.95],"2":[0.94,0.91,0.91,0.9],"3":[1.04,1.03,1.03,1.03],
    "4":[1.01,1.03,1.04,1.65],"5":[1.04,1.08,1.11,1.13],"6":[1.01,1.06,1.08,1.11],"7":[1.04,1.08,1.12,1.14],"8":[1.04,1.07,1.08,1.12],
    "9":[1.01,1.02,1.02,1.02],"10":[1.04,1.02,1.01,1],"11":[1.01,0.98,0.95,0.93],"12":[1.04,0.99,0.97,0.94]}
    df2 = pd.DataFrame(data2)
    
    df2 = df2.set_index('lat')
    index_set = df2.index.unique()
    
    for i in range(20):
        if i not in index_set:
            df2.loc[i] = pd.Series()
    
    df2 = df2.sort_values(by=['lat'])
    
    res_df = df2.interpolate()
    
    tdf = pd.read_csv('try.tsv', sep='\s+', header=None, index_col=None)
    tdf.columns = ['id', 'lat', 'year', 'month']
    tdf['lat'] = tdf.lat.astype(int)
    
    tdf['N'] = tdf.apply(lambda x: res_df.loc[x['lat'], str(x['month'])], axis=1)
    print(tdf)
    

    输出:

        id  lat  year  month      N
    0    0   11  2000      1  0.994
    1    1    9  2000      2  0.913
    2    2   11  2000      3  1.030
    3    3   10  2000      4  1.030
    4    4   17  2000      5  1.118
    5    5    6  2000      6  1.040
    6    6   18  2000      7  1.132
    7    7   11  2000      8  1.072
    8    8   17  2000      9  1.020
    9    9   12  2000     10  1.016
    10  10   19  2000     11  0.934
    11  11    8  2000     12  1.000
    12  12   14  2001      1  0.976
    13  13   13  2001      2  0.910
    14  14   14  2001      3  1.030
    15  15   12  2001      4  1.034
    16  16   12  2001      5  1.092
    17  17    5  2001      6  1.035
    18  18   18  2001      7  1.132
    19  19   13  2001      8  1.076
    20  20    7  2001      9  1.017
    21  21   18  2001     10  1.004
    22  22   12  2001     11  0.968
    23  23   10  2001     12  0.990
    24  24   14  2002      1  0.976
    25  25   14  2002      2  0.910
    26  26   20  2002      3  1.030
    27  27   20  2002      4  1.650
    28  28    9  2002      5  1.076
    29  29   15  2002      6  1.080
    30  30   15  2002      7  1.120
    31  31    5  2002      8  1.055
    32  32    7  2002      9  1.017
    33  33    5  2002     10  1.030
    34  34    6  2002     11  0.992
    35  35    7  2002     12  1.005
    

    【讨论】:

    • 谢谢,但是如何在主数据集中创建 N 列并根据纬度和月份定义值?
    • @KaSan 更新了答案以获取您需要的值
    猜你喜欢
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多