如何在数据框中创建其他行的平均值行?

2024-10-01 15:44:38 发布

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

以这样的数据帧为例:

import pandas as pd
info = {'Year': [2010, 2010, 2010, 2010, 2015, 2015, 2015, 2015],
        'Country': ['USA', 'Mexico', 'Canada', 'China', 'USA', 'Mexico', 'Canada', 'China'],
        'AgeAvg': [40, 44, 45, 49, 45, 46, 50, 52],
        'HeightAvg': [68, 65, 67, 68, 69, 70, 64, 67]}
df = pd.DataFrame(data=info)
df

   Year Country  AgeAvg  HeightAvg
0  2010     USA      40         68
1  2010  Mexico      44         65
2  2010  Canada      45         67
3  2010   China      49         68
4  2015     USA      45         69
5  2015  Mexico      46         70
6  2015  Canada      50         64
7  2015   China      52         67

我想为2011、2012、2013和2014添加行。这些行将跟随相同的国家,并且具有平滑的变量平均值。例如,2011年美国年龄为41岁,2012年美国年龄为42岁,2013年美国年龄为43岁,2014年美国年龄为44岁。这样,这个年龄将从2010年到2015年。我也希望对所有变量(比如本例中的身高)都这样做,而不仅仅是年龄。有没有一种方法可以在Python中对熊猫执行此操作


Tags: 数据importinfopandasdfyearcountrypd
2条回答

使用pd.MultiIndex.from_product重新索引数据帧并插值:

mi = pd.MultiIndex.from_product([df['Country'].unique(),
                                 range(df.Year.min(), df.Year.max()+1)])

out = df.set_index(['Country', 'Year']).reindex(mi)
out = out.groupby(level=0).apply(lambda x: x.interpolate())
>>> out
             AgeAvg  HeightAvg
USA    2010    40.0       68.0
       2011    41.0       68.2
       2012    42.0       68.4
       2013    43.0       68.6
       2014    44.0       68.8
       2015    45.0       69.0
Mexico 2010    44.0       65.0
       2011    44.4       66.0
       2012    44.8       67.0
       2013    45.2       68.0
       2014    45.6       69.0
       2015    46.0       70.0
Canada 2010    45.0       67.0
       2011    46.0       66.4
       2012    47.0       65.8
       2013    48.0       65.2
       2014    49.0       64.6
       2015    50.0       64.0
China  2010    49.0       68.0
       2011    49.6       67.8
       2012    50.2       67.6
       2013    50.8       67.4
       2014    51.4       67.2
       2015    52.0       67.0

如果您喜欢先Year,可以交换级别

out = out.swaplevel().sort_index()
  • 生成所有年份的所有组合
  • merge()以拥有所有行
  • interpolate()每个国家(groupby()
pd.DataFrame(
    {
        "Year": range(df["Year"].min(), df["Year"].max()+1),
        "Country": [df["Country"].unique() for y in range(df["Year"].min(), df["Year"].max()+1)],
    }
).explode("Country").merge(df, on=["Year", "Country"], how="outer").groupby(
    "Country"
).apply(
    lambda d: d.interpolate()
)

^{tb1}$

相关问题 更多 >

    热门问题