Reputation: 170
I have 2 dataframes :
ID word
1 srv1
2 srv2
3 srv1
4 nan
5 srv3
6 srv1
7 srv5
8 nan
ID word
1 nan
2 srv12
3 srv10
4 srv8
5 srv4
6 srv7
7 nan
8 srv9
What I need is to merge thoses 2 dataframes on ID and combine the column word to get :
ID word
1 srv1
2 srv2 , srv12
3 srv1 , srv10
4 srv8
5 srv3 , srv4
6 srv1 , srv7
7 srv5
8 srv9
With the following code
merge = pandas.merge(df1,df2,on="ID",how="left")
merge["word"] = merge[word_x] + " , " + merge["word_y"]
I am getting:
ID word
1 nan
2 srv2 , srv12
3 srv1 , srv10
4 nan
5 srv3 , srv4
6 srv1 , srv7
7 nan
8 nan
Which it is not the correct solution.
Upvotes: 4
Views: 214
Reputation: 13393
you can use np.select
to select the existing value, or the concatenated value.
try this:
import pandas as pd
import numpy as np
from io import StringIO
df1 = pd.read_csv(StringIO("""
ID word
1 srv1
2 srv2
3 srv1
4 nan
5 srv3
6 srv1
7 srv5
8 nan"""), sep=r"\s+")
df2 = pd.read_csv(StringIO("""
ID word
1 nan
2 srv12
3 srv10
4 srv8
5 srv4
6 srv7
7 nan
8 srv9"""), sep=r"\s+")
conditions = [(~df1["word"].isna()) & df2["word"].isna(), df1["word"].isna() & (~df2["word"].isna()), (~df1["word"].isna()) & (~df2["word"].isna())]
choices = [df1["word"], df2["word"], df1["word"] + "," + df2["word"]]
df1["word"] = np.select(conditions,choices)
print(df1)
Output:
ID word
0 1 srv1
1 2 srv2,srv12
2 3 srv1,srv10
3 4 srv8
4 5 srv3,srv4
5 6 srv1,srv7
6 7 srv5
7 8 srv9
Upvotes: 1
Reputation: 408
Based on what I think you want to do I would first get rid of those nan
's:
df_1.fillna(value="")
df_2.fillna(value="")
And then I would try the merge again and see if you get what you want.
Upvotes: 0
Reputation: 4011
You can use Series.str.cat
and the na_rep
option to populate the word
column even if one of the source columns in nan
, then use str.strip
to trim any leading/trailing ' , '
not between words.
m['word'] = m['word_x'].str.cat(m['word_y'], sep=' , ', na_rep='').str.strip(' , ')
returns
ID word_x word_y word
0 1 srv1 NaN srv1
1 2 srv2 srv12 srv2 , srv12
2 3 srv1 srv10 srv1 , srv10
3 4 NaN srv8 srv8
4 5 srv3 srv4 srv3 , srv4
5 6 srv1 srv7 srv1 , srv7
6 7 srv5 NaN srv5
7 8 NaN srv9 srv9
Upvotes: 5