Naman Shah
Naman Shah

Reputation: 97

Conditional merge on in pandas

My question in simple I am using pd.merge to merge two df . Here's the line of code:

pivoted = pd.merge(pivoted, concerned_data, on='A')

and I want the on='B' whenever a row has column A value as null. Is there a possible way to do this?

Edit:

As an example if

 df1:   A | B |randomval
        1 | 1 |   ty
       Nan| 2 | asd
 df2:   A | B  |randomval2
        1 | Nan|   tyrte
        3 | 2  | asde

So if on='A' and the value is Nan is any of the df (for a single row) I want on='B' for that row only

Thank you!

Upvotes: 0

Views: 208

Answers (1)

RK1
RK1

Reputation: 2532

You could create a third column in your pandas.DataFrame which incorporates this logic and merge on this one.

For example, create dummy data

df1 = pd.DataFrame({"A" : [1, None], "B" : [1, 2], "Val1" : ["a", "b"]})
df2 = pd.DataFrame({"A" : [1, 2], "B" : [None, 2], "Val2" : ["c", "d"]})

Create a column c which has this logic

df1["C"] = pd.concat([df1.loc[~df1.A.isna(), "A"], df1.loc[df1.A.isna(), "B"]],ignore_index=False)
df2["C"] = pd.concat([df2.loc[~df2.A.isna(), "A"], df2.loc[df2.A.isna(), "B"]],ignore_index=False)

Finally, merge on this common column and include only your value columns

df3 = pd.merge(df1[["Val1","C"]], df2[["Val2","C"]], on='C')

In [27]: df3
Out[27]:
  Val1    C Val2
0    a  1.0    c
1    b  2.0    d

Upvotes: 1

Related Questions