Skip to content
Advertisement

Create DF Columns Based on Second DDF

I have 2 dataframes with different columns:

JavaScript

I would like to add the missing columns for the 2 dataframes – so each one will have each own columns + the other DFs columns (without column “number”). And the new columns will have initial number for our choice (let’s say 0).
So the final output:

JavaScript

What’s the best way to achieve this result? I’ve got messed up with getting the columns and trying to create new ones.

Thank!

Advertisement

Answer

First, you need to create a superset of all the columns which are present in both the dataframes. This you can do using the below code.

JavaScript

Then for each dataframes, you need to check which columns are missing that you can do using the below code.

JavaScript

Then add the missing columns in both the dataframes

JavaScript

Hope this solves your query!

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