1

I am trying to build a master DataFrame over time. Lets say its imported from a csv file and looks something like this:

df = pd.DataFrame(columns=['col1', 'col2', 'col3', 'col4']).rename_axis='Date'

At some point in time I will have a DataFrame like this, that I will append to df.

dfb = pd.DataFrame(columns=['col1', 'col2']).rename_axis('Date')

At a later point in time I will have another DataFrame, call it dfe, containing the remaiming col3 and col4.

dfe = pd.DataFrame(columns=['col3', 'col4']).rename_axis('Date')

How do I add dfe to df such that it fills in on the same row and does not generate additional columns. I tried:

df = pd.concat([df, dfe], axis=1)

but new columns are generated. I think this is because the previous append step put NaN values in col3 and col4.

UPDATE: I asked this in a somewhat different way and recieved great answers. See the post here.

How to replace missing data in DataFrame

SamCie
  • 117
  • 1
  • 7
  • Does this answer your question? [link](https://stackoverflow.com/questions/34504033/dataframe-append-a-row) – Raphael Nov 08 '19 at 14:12

1 Answers1

0

You will have to use the df.merge(dfe) function. This will merge your columns according to the date index. Bare in mind that, in case of conflicts you can decide which value will "win".

Lior Cohen
  • 5,570
  • 2
  • 14
  • 30