aitor1717
aitor1717

Reputation: 11

Pandas: Use a dataframe to index another one and fill the gaps?

I have two dataframes. df_0 is a complete list of dates and df_1 is a generic register indexed by incomplete dates. I need to make a dataframe that has df_0’s complete dates as an index, filled with df_1’s register in the matching dates. For dates without a register entry, I just need to repeat the last date’s register data as a filler. Any ideas on how to make this?

Thanks in advance.

Upvotes: 1

Views: 162

Answers (2)

jezrael
jezrael

Reputation: 862691

Use DataFrame.reindex with parameter method:

df = df_1.reindex(df_0.index, method="ffill")

Upvotes: 2

Woods Chen
Woods Chen

Reputation: 620

use 'reindex' to expand the df_1 and use 'fillna' to fill in the missed valued.

df_2 = df_1.reindex(df_0.index).fillna(method="ffill")

Upvotes: 0

Related Questions