创建数学数据插补函数

2024-04-24 08:18:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在执行许多类似的操作,我想编写一个函数,但甚至不知道如何实现这一点。我正在为以下系列计算0数据的值:

  • 公式为2*2001年的值-2002年的值

我现在用Python一个接一个地做:

print(full_data.loc['Croatia', 'fertile_age_pct'])
print(full_data.loc['Croatia', 'working_age_pct'])
print(full_data.loc['Croatia', 'young_age'])
print(full_data.loc['Croatia', 'old_age'])

full_data.replace(to_replace={'fertile_age_pct': {0:(2*46.420061-46.326103)}}, inplace=True)
full_data.replace(to_replace={'working_age_pct': {0:(2*67.038157-66.889212)}}, inplace=True)
full_data.replace(to_replace={'young_age': {0:(2*0.723475-0.715874)}}, inplace=True)
full_data.replace(to_replace={'old_age': {0:(2*0.692245-0.709597)}}, inplace=True)

数据帧(完整数据):

geo_full  year   fertile_age_pct    working_age_pct    young_age    old_age
Croatia   2000   0                  0                  0            0
Croatia   2001   46.420061          67.038157          0.723475     0.692245
Croatia   2002   46.326103          66.889212          0.715874     0.709597
Croatia   2003   46.111822          66.771187          0.706091     0.72444
Croatia   2004   45.929829          66.782133          0.694854     0.735333
Croatia   2005   45.695932          66.742514          0.686534     0.747083

1条回答
网友
1楼 · 发布于 2024-04-24 08:18:31

因此,您试图用公式填充2000年的0值。如果数据框中有其他国家,则可能会变得混乱

假设每个国家的第一年都是0,请尝试以下方法:

full_data.set_index('year', inplace=True)
fixed_data = {}
for country, df in full_data.groupby('geo_full')[full_data.columns[1:]]:
    if df.iloc[0].sum() == 0:
        df.iloc[0] = df.iloc[1] * 2 - df.iloc[0]
    fixed_data[country] = df
fixed_data = pd.concat(list(fixed_data.values()), keys=fixed_data.keys(), names=['geo_full'], axis=0)

相关问题 更多 >