0

I have two dataframes that look like these:

scores = [0.511, 0.276, -0.107, -0.04, -0.02, -0.03]
df1 = pd.DataFrame(scores, columns=['scores'])
df1['TR_Time'] = [290, 498, 810, 1164, 1, 2]
df1['TR'] = [1,2,3,4,5,6]

The second one looks like this:

event = ['SRNeg', 'SINeu', 'SINeg', 'SINeg']
df2 = pd.DataFrame(event, columns=['event'])
df2['Onset'] = [291.79, 499.31, 810.93, 1164.25]
df2['Onset_TR_Time'] = [290, 498, 810, 1164]

I want to align the Onset_TR_Time in df2 with TR_Time in df1 such that df1 has an extra column for event. If there is no corresponding TR_Time in df1 with Onset_TR_Time in df2, then 'NA' should be filled in for event in the final df1. This is the example output:

scores = [0.511, 0.276, -0.107, -0.04, -0.02, -0.03]
df1 = pd.DataFrame(scores, columns=['scores'])
df1['TR_Time'] = [290, 498, 810, 1164,1,2]
df1['TR'] = [1,2,3,4,5,6]
df1['event'] = ['SRNeg','SINeu','SINeg','SINeg', 'NA', 'NA']

Does anyone know an elegant way to do this?

Thank you!

jo_
  • 677
  • 2
  • 11

0 Answers0