1

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
---------- ---------------- ----
Jack
  • 181
  • 10
  • 1
    Does this answer your question? [Pandas merge by condition](https://stackoverflow.com/questions/69090245/pandas-merge-by-condition) – LSeu Aug 01 '22 at 09:21
  • Here, unique id is not present. Need to merge using the comparision of rows. If start and end of rows are same on both of the data set. Then only need to merge the rows – Jack Aug 01 '22 at 09:38

1 Answers1

1
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

Deven Ramani
  • 751
  • 4
  • 10