好吧,我是Python的新手,我被下面的问题困住了。我有一个DF作为:
ipdb>;数据框
asofdate port_id
1 2010-01-01 76
2 2010-04-01 43
3 2011-02-01 76
4 2013-01-02 93
5 2017-02-01 43
对于年度缺口,比如2012年、2014年、2015年和2016年,我想用缺失年份的新年日期和上一年的港口id来填补缺口。理想情况下,我想:
ipdb>;数据框
asofdate port_id
1 2010-01-01 76
2 2010-04-01 43
3 2011-02-01 76
4 2012-01-01 76
5 2013-01-02 93
6 2014-01-01 93
7 2015-01-01 93
8 2016-01-01 93
9 2017-02-01 43
我尝试了多种方法,但仍然没有结果。有专家能告诉我怎么做吗?提前多谢!你知道吗
您可以使用
set.difference
和range
来查找缺少的日期,然后附加数据帧:我将创建一个
helper
数据框,其中包含所有的年份开始日期,然后过滤出与df
中年份匹配的年份,最后将它们合并在一起:相关问题 更多 >
编程相关推荐