繁体   English   中英

将“缺失”行添加到多索引组 pandas dataframe

[英]Add "missing" rows to multi-index groupby pandas dataframe

我有一个 DataFrame,看起来像这样:

                         numberSold        
date | location | time
3/10      FL      12:00        4  
                  1:00         1
                  4:00         5  
3/11      FL      1:00         2
                  2:00         3
                  3:00         0
3/12      FL      2:00         6
                  5:00         6

它是多索引(日期、位置、时间)。 我希望 output 如下所示:

                         numberSold        
date | location | time
3/10      FL      12:00        4
                  1:00         1
                  4:00         5    
3/11      FL      12:00        4
                  1:00         2
                  2:00         3
                  3:00         0
                  4:00         5  
3/12      FL      12:00        4
                  1:00         2
                  2:00         6
                  3:00         0
                  5:00         6

这是字典格式的第一个 DataFrame:

{'numberSold': {('3/10', 'FL', '12:00'): 4,
  ('3/10', 'FL', '1:00'): 1,
  ('3/10', 'FL', '4:00'): 5,
  ('3/11', 'FL', '1:00'): 2,
  ('3/11', 'FL', '2:00'): 3,
  ('3/11', 'FL', '3:00'): 0,
  ('3/12', 'FL', '2:00'): 6,
  ('3/12', 'FL', '5:00'): 6}}

基本上,我希望该表基于以前的条目构建。 如果该条目存在于当前条目中,则使用当前条目(如 3/11 1:00 使用“2”而不是“1”),但如果不存在,则只需添加上一行有(比如 3/11 如何从 3/10 获得 4:00 的值)。

我不确定如何使用 Pandas 来做这样的事情,我觉得它很简单,但我的尝试都失败了。

您可以pivot + ffill来获取丢失的数据; 然后stack以将 DataFrame 恢复为之前的形状:

df.index.names = ['date', 'location', 'time']
out = df.reset_index().pivot(['date', 'location'], 'time', 'numberSold').ffill().stack().to_frame(name='numberSold')

Output:

                     numberSold
date location time             
3/10 FL       12:00         4.0
              1:00          1.0
              4:00          5.0
3/11 FL       12:00         4.0
              1:00          2.0
              2:00          3.0
              3:00          0.0
              4:00          5.0
3/12 FL       12:00         4.0
              1:00          2.0
              2:00          6.0
              3:00          0.0
              4:00          5.0
              5:00          6.0

暂无
暂无

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

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