Home > Software design >  Update a Dataframe with more columns with a Dataframe with less columns
Update a Dataframe with more columns with a Dataframe with less columns

Time:05-13

In pandas, it's possible to update two dataframes when they have equal columns. In my case, they is a need to update a dataframe with more columns by df with fewer columns. Is this possible?. For clarity below is an illustration of the same

df1 (the dataframe with fewer columns)

df1 = pd.DataFrame([["Mary", "Tobias", "Linus"], [16, 14, 10], [16, 17, 20]])

df2, the parent dataframe that needs to be updated, has more columns

df2 = pd.DataFrame([["Mary", "Tobias", "Linus", "Tonny", "Angela"], []])

Desired output

0  Mary  Tobias  Linus  Tonny  Angela
1    16     14     10   None    None
2    16      17    20   None    None

CodePudding user response:

The operation you're looking for is an outer merge or in pandas:

pd.merge(df1, df2,  how='outer')

This will add an extra column of Nones at the end because your current definition of df2 has one empty entry (which defaults to all cols as None).

  • Related