Skip to content
Advertisement

How to merge two dataframes where the second one has different column names and length?

I have two dataframes. The first one is just a column of daily datetime, whereas the second one has both dates and data. This is an example:

JavaScript

What I want to do is to merge df1 and df2 to get a dataframe (dataset) where: when the data exist it takes the date position; when it doesn’t exist, it just gets NaN. This is what I did:

JavaScript

Unfortunately, I get only NaNs or NaTs everywhere.

Instead, I would like to get:

JavaScript

Can anyone help me with this?

Thanks!

Advertisement

Answer

If there si same pattern – datetimes with data columns is possible use list comprehension with concat, then convert values to datetimes in index and DataFrame.merge by first DataFrame (by index name Date and column name Date):

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement