Search code examples
pythonpandasdata-analysis

Merge column based on condition in pandas


How to add a new column value based on condition?Having two data set as follows: First data set contain 2 columns as follows:

Start End
A B
A C
A D
B A
B C
B E
---------- ----------------

Second data set contain 3 columns.

start End time
A B 8
A D 9
A E 10
B A 7
B E 4
---------- ---------------- ----

If the start and end are same, add the time with the first data set.How to merge these two columns in python as follows.

Start End Time
A B 8
A C nan
A D 9
B A 7
B C nan
B E 4
---------- ---------------- ----

Solution

  • df1 = pd.DataFrame({'Start':['A', 'A', 'A', 'B', 'B', 'B'],
                       'End': ['B', 'C', 'D', 'A', 'C', 'E']})
    df2 = pd.DataFrame({'Start':['A', 'A', 'A', 'B', 'B'],
                       'End': ['B', 'D', 'E', 'A', 'E'],
                       'time':[ 8,  9, 10,  7,  4]})
    result = df1.merge(df2, how='left')
    
    Start End time
    A B 8
    A C nan
    A D 9
    B A 7
    B C nan
    B E 4

    Here I am assuming that your both dataframe saving same column name as Start and End