【发布时间】:2023-03-30 07:34:01
【问题描述】:
我有一个df:
company year revenues
0 company 1 2019 1,425,000,000
1 company 1 2018 1,576,000,000
2 company 1 2017 1,615,000,000
3 company 1 2016 1,498,000,000
4 company 1 2015 1,569,000,000
5 company 2 2019 nan
6 company 2 2018 1,061,757,075
7 company 2 2017 nan
8 company 2 2016 573,414,893
9 company 2 2015 599,402,347
我想fillnan 的值,有一个订单。我想先线性插值,然后是前向填充,然后是后向填充。我目前有:
f_2_impute = [x for x in cl_data.columns if cl_data[x].dtypes != 'O' and 'total' not in x and 'year' not in x]
def ffbf(x):
return x.ffill().bfill()
group_with = ['company']
for x in cl_data[f_2_impute]:
cl_data[x] = cl_data.groupby(group_with)[x].apply(lambda fill_it: ffbf(fill_it))
执行ffill() 和bfill()。理想情况下,我想要一个函数,它首先尝试线性插入缺失值,然后尝试向前填充它们,然后向后填充它们。
有什么快速的方法吗?提前谢谢你。
【问题讨论】:
标签: python pandas interpolation nan missing-data