2

I have a simple question best described with an example. I am trying to render output_df based on current_df and parent_df.

current_df: simple time series df with datetime index

Index                            Val
'10-1-2010 08:00:00'              1.23
'10-1-2010 09:00:00'              1.3
'10-1-2010 10:00:00'               1.43

parent_df: another simple time series df

Index                            Val
'10-1-2010 07:00:00'              0.23
'10-1-2010 08:00:00'              1.23
'10-1-2010 09:00:00'              1.3
'10-1-2010 10:00:00'               1.43
'10-1-2010 11:00:00'              2.23

The output_df should:

  1. contain index of parent_df
  2. contain val of 0 if index not in current_df
Index                             Val
'10-1-2010 07:00:00'              0
'10-1-2010 08:00:00'              1.23
'10-1-2010 09:00:00'              1.3
'10-1-2010 10:00:00'               1.43
'10-1-2010 11:00:00'                0

This should be an easy task - I'm just blanking.

Cheers.

CypherX
  • 7,019
  • 3
  • 25
  • 37
BeardedDork
  • 162
  • 2
  • 13

3 Answers3

1

I think it is the functionality of reindex

output_df = current_df.reindex(parent_df.index, fill_value=0)
Andy L.
  • 24,909
  • 4
  • 17
  • 29
1

you can do it using merge like below

parent_df[["Index"]].merge(current_df, on="Index", how="left").fillna(0)
Dev Khadka
  • 5,142
  • 4
  • 19
  • 33
1

I think this code snippet will help you.

# copy the dataframe
output_df = parent_df

# use negated .isin() search to find the indices that are not in current_df
# and replace them with zero
output_df.loc[~output_df['Index'].isin(current_df['Index'])] = 0
lobetdenherrn
  • 303
  • 1
  • 8