Reputation: 543
I have a dataframe named "table":
UNICO | RES |
Responsabile| - |
Product | Prodotto|
Brand | Brand |
The column names of table dataframe corresponds to 2 dataframe. The first one, "Unico", which is empty, is something like this:
Responsabile | Product | Brand
NaN | NaN | NaN
The second one, "RES" is something like this:
Prodotto | Brand
X | AA
Y | BB
I want to use the rows of the dataframe table to connect the values of the columns of the dataframe UNICO to the values of the columns of the dataframe RES and populate the dataframe UNICO. My expected outcome is:
UNICO:
Responsabile | Product | Brand
NaN | X | AA
NaN | Y | BB
Upvotes: 0
Views: 49
Reputation: 56
To solve this situation, there is two main paths:
import pandas as pd
import numpy as np
unico = pd.DataFrame({'Resposabile':[np.nan], 'Product':[np.nan], 'Brand':[np.nan]})
res = pd.DataFrame({'Prodotto':['x', 'y'], 'Brand':['AA', 'BB']})
output = pd.merge(left=unico,
right=res,
how='outer',
left_index=True,
right_index=True)
output = output[['Resposabile', 'Prodotto', 'Brand_y']]
output.columns = ['Resposabile', 'Prodotto', 'Brand']
This solution implies an outer join. Bechas the column Brand appears in both DataFrames, at the end of the day the one retaines is the one with non-nulls values.
output = pd.concat([unico.Resposabile, res], axis=1)
Upvotes: 1