Jesse Pet
Jesse Pet

Reputation: 1879

Vertical Merging of Pandas Series

I currently have two dataframes that share a common column like so:

df1 = 
    Text    Val
    "This"     1
    "That"     2
    "Is"       3
    "Not"      ''
    "Working"  ''

df2 = 
    Text    Val
    "This"     ''
    "That"     ''
    "Is"       ''
    "Not"      4
    "Working"  5

I want to merge the two dataframes in the following manner:

merged_df = 
    Text    Val
    "This"     1
    "That"     2
    "Is"       3
    "Not"      4
    "Working"  5

Essentially, the two dataframes, df1 and df2 both share a common "Text" column. All of the values are similar. However, in df1, there are no values for the rows corresponding to "Not" and "Working", but these are available in df2.

How would one approach stacking these together to come up with merged_df?

The examples are trivial compared to my actual data, but hopefully this helps

Upvotes: 2

Views: 337

Answers (1)

jezrael
jezrael

Reputation: 862691

You can use combine_first with set_index for align data and replace to NaNs (if necessary):

df1['Val'] = df1['Val'].replace("''",np.nan)
df2['Val'] = df2['Val'].replace("''",np.nan)

df = df1.set_index('Text').combine_first(df2.set_index('Text')).reset_index()
print (df)
      Text Val
0     This   1
1     That   2
2       Is   3
3      Not   4
4  Working   5

Upvotes: 3

Related Questions