Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当索引值不存在时,在多索引数据帧中传播值_Python_Pandas_Multi Index_Reindex - Fatal编程技术网

Python 当索引值不存在时,在多索引数据帧中传播值

Python 当索引值不存在时,在多索引数据帧中传播值,python,pandas,multi-index,reindex,Python,Pandas,Multi Index,Reindex,我有一个由groupby()生成的多索引数据帧,如下所示: df_grouped = df.groupby(['date', 'name']).agg({'ABC': 'sum'}) df_grouped ABC date name 01-03-2018 Adam 1 John 2 01-04-2018 Adam 4 Sam 1 01-05-2018 Adam 5

我有一个由
groupby()
生成的多索引数据帧,如下所示:

df_grouped = df.groupby(['date', 'name']).agg({'ABC': 'sum'})
df_grouped
                   ABC
date        name 
01-03-2018  Adam   1
            John   2
01-04-2018  Adam   4
            Sam    1
01-05-2018  Adam   5
            John   3
            Sam    2
01-06-2018  Jake   1
仅当新的
日期中不存在
名称时,我才希望在
日期中向前传播ABC值。如果存在,则应保持原样:

                   ABC
date        name 
01-03-2018  Adam   1
            John   2
01-04-2018  Adam   4
            John   2
            Sam    1
01-05-2018  Adam   5
            John   3
            Sam    2
01-06-2018  Jake   1
            Adam   5
            John   3
            Sam    2
我不知道如何在不循环每个
日期的情况下有效地执行此操作。请问有没有更好的办法

df = df_grouped.unstack().ffill().stack().astype(int)
                 ABC
date       name     
01-03-2018 Adam    1
           John    2
01-04-2018 Adam    4
           John    2
           Sam     1
01-05-2018 Adam    5
           John    3
           Sam     2
01-06-2018 Adam    5
           Jake    1
           John    3
           Sam     2
IIUC


谢谢你的解决方案。我试过了,效果很好。谢谢你的解决方案。我试过了,效果很好。