'Update a Dataframe with more columns with a Dataframe with less columns
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
Solution 1:[1]
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).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | monk |
