我有一个熊猫数据框,如下所示:
Timestamp Player Rotated Lat Rotated Lon
2018-11-11 16:22:21.999993600 G -15.89769 84.714795
2018-11-11 16:22:21.999993600 W -15.897637 84.714784
2018-11-11 16:22:21.999993600 K -15.897617 84.714621
2018-11-11 16:22:21.999993600 Y -15.897638 84.714787
2018-11-11 16:22:22.099958400 K -15.897618 84.714623
2018-11-11 16:22:22.099958400 Y -15.897691 84.714796
2018-11-11 16:22:22.099958400 W -15.897619 84.714626
2018-11-11 16:22:22.200009600 Y -15.897693 84.714794
2018-11-11 16:22:22.200009600 G -15.897639 84.714788
2018-11-11 16:22:22.200009600 K -15.897693 84.714802
2018-11-11 16:22:22.299974400 W -15.897692 84.714796
2018-11-11 16:22:22.299974400 G -15.897622 84.714629
2018-11-11 16:22:22.299974400 Y -15.897639 84.714791
2018-11-11 16:22:22.299974400 K -15.897694 84.714799
2018-11-11 16:22:22.400025600 G -15.89764 84.714794
2018-11-11 16:22:22.400025600 K -15.897622 84.714632
2018-11-11 16:22:22.400025600 Y -15.897692 84.714804
2018-11-11 16:22:22.400025600 W -15.897623 84.714635
2018-11-11 16:22:22.499990400 Y -15.897692 84.714806
2018-11-11 16:22:22.499990400 W -15.897694 84.714802
2018-11-11 16:22:22.499990400 G -15.897641 84.714795
2018-11-11 16:22:22.499990400 K -15.897694 84.714808
如果您注意到,我有4个玩家:G,W,K,Y。因此每个时间戳记索引中应该有4个。但是,缺少一些时间戳。我该如何添加所有缺少的时间戳,然后向前填充其他值,以仅获取不在给定时间戳中的那些玩家?
例如,对于时间戳2018-11-11 16:22:22.099958400
,缺少玩家G。我该如何为那个球员填补?
所需的输出(我已将框架隔开以使其更具可读性):
Timestamp Player Rotated Lat Rotated Lon
2018-11-11 16:22:21.999993600 G -15.89769 84.714795
2018-11-11 16:22:21.999993600 W -15.897637 84.714784
2018-11-11 16:22:21.999993600 K -15.897617 84.714621
2018-11-11 16:22:21.999993600 Y -15.897638 84.714787
2018-11-11 16:22:22.099958400 K -15.897618 84.714623
2018-11-11 16:22:22.099958400 Y -15.897691 84.714796
2018-11-11 16:22:22.099958400 W -15.897619 84.714626
2018-11-11 16:22:22.099958400 G -15.89769 84.714795
2018-11-11 16:22:22.200009600 Y -15.897693 84.714794
2018-11-11 16:22:22.200009600 G -15.897639 84.714788
2018-11-11 16:22:22.200009600 K -15.897693 84.714802
2018-11-11 16:22:22.200009600 W -15.897619 84.714626
答案 0 :(得分:4)
将set_index
与unstack
一起使用以进行整形,向前填充丢失的值,最后通过stack
重新整形:
df = df.set_index('Player', append=True).unstack().ffill().stack().reset_index(level=1)
print (df)
Player Rotated Lat Rotated Lon
Timestamp
2018-11-11 16:22:21.999993600 G -15.897690 84.714795
2018-11-11 16:22:21.999993600 K -15.897617 84.714621
2018-11-11 16:22:21.999993600 W -15.897637 84.714784
2018-11-11 16:22:21.999993600 Y -15.897638 84.714787
2018-11-11 16:22:22.099958400 G -15.897690 84.714795
2018-11-11 16:22:22.099958400 K -15.897618 84.714623
2018-11-11 16:22:22.099958400 W -15.897619 84.714626
2018-11-11 16:22:22.099958400 Y -15.897691 84.714796
2018-11-11 16:22:22.200009600 G -15.897639 84.714788
2018-11-11 16:22:22.200009600 K -15.897693 84.714802
2018-11-11 16:22:22.200009600 W -15.897619 84.714626
2018-11-11 16:22:22.200009600 Y -15.897693 84.714794
2018-11-11 16:22:22.299974400 G -15.897622 84.714629
2018-11-11 16:22:22.299974400 K -15.897694 84.714799
2018-11-11 16:22:22.299974400 W -15.897692 84.714796
2018-11-11 16:22:22.299974400 Y -15.897639 84.714791
2018-11-11 16:22:22.400025600 G -15.897640 84.714794
2018-11-11 16:22:22.400025600 K -15.897622 84.714632
2018-11-11 16:22:22.400025600 W -15.897623 84.714635
2018-11-11 16:22:22.400025600 Y -15.897692 84.714804
2018-11-11 16:22:22.499990400 G -15.897641 84.714795
2018-11-11 16:22:22.499990400 K -15.897694 84.714808
2018-11-11 16:22:22.499990400 W -15.897694 84.714802
2018-11-11 16:22:22.499990400 Y -15.897692 84.714806