Python Pandas: How to merge based on an “OR” condition?

后端 未结 2 1834
甜味超标
甜味超标 2021-02-13 03:55

Let\'s say I have two dataframes, and the column names for both are:

table 1 columns:
[ShipNumber, TrackNumber, ShipDate, Quantity, Weight]
table 2 columns:
[Shi         


        
2条回答
  •  佛祖请我去吃肉
    2021-02-13 04:14

    I would suggest this alternate way for doing merge like this. This seems easier for me.

    table1["id_to_be_merged"] = table1.apply(
        lambda row: row["ShipNumber"] if pd.notnull(row["ShipNumber"]) else row["TrackNumber"], axis=1)
    

    You can add the same column in table2 as well if needed and then use in left_in or right_on based on your requirement.

提交回复
热议问题