简体   繁体   English

熊猫数据框添加缺少的时间戳行,然后向前填充以前的值

[英]Pandas Dataframe add missing timestamp row and then forward fill previous values

I have a pandas dataframe as follows: 我有一个熊猫数据框,如下所示:

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

If you notice, I have 4 players: G, W, K, Y. Therefore there should be 4 of each timestamp index. 如果您注意到,我有4个玩家:G,W,K,Y。因此每个时间戳记索引中应该有4个。 However, some Timestamps are missing. 但是,缺少一些时间戳。 How can I add all the missing timestamps and then forward fill the other values to get only those players who are not in a given timestamp? 如何添加所有缺少的时间戳,然后向前填充其他值,以仅获取不在给定时间戳中的那些球员?

For example, for timestamp 2018-11-11 16:22:22.099958400 , Player G is missing. 例如,对于时间戳2018-11-11 16:22:22.099958400 ,玩家G丢失了。 How can I fill for just that player? 我该如何填补这个球员?

Desired output (I have spaced the frame to make it more readable): 所需的输出(我已将框架隔开以使其更具可读性):

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

Use set_index with unstack for reshape, forward fill missing values and last reshape back by stack : 使用set_indexunstack的重塑,向前填补缺失值和最后重塑背部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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM