I have a multi-index dataframe resulting from a groupby()
as follows:
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
I want to propagate the ABC values forward in date
only if the name
does not exist in the new date
. If it exists, then it should be left as it is:
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
I am not sure how to do this efficiently without looping over each date
. Is there a better way, please?
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