我正在尝试将日期填入组(“类型”)并回填新行。我已经看到了使用reindex的示例,但是我不确定如何在维护组的同时填充这些日期。
在:
type date value
a 2016-01-01 1
a 2016-01-04 3
b 2016-01-10 4
b 2016-01-13 7
想要的:
type date value
a 2016-01-01 1
a 2016-01-02 3
a 2016-01-03 3
a 2016-01-04 3
b 2016-01-10 4
b 2016-01-11 7
b 2016-01-12 7
b 2016-01-13 7
df.set_index('date').groupby('type', as_index=False).resample('d').bfill().reset_index().drop('level_0', axis=1)
Out:
date type value
0 2016-01-01 a 1
1 2016-01-02 a 3
2 2016-01-03 a 3
3 2016-01-04 a 3
4 2016-01-10 b 4
5 2016-01-11 b 7
6 2016-01-12 b 7
7 2016-01-13 b 7
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句