Filter a data frame with similar o near time values (vlookup in dataframe)

Viewed 47

I'd like to merge two data frames with near times values leaving one index fixed to search in the other data frame (similar to vlookup in excel). Can you recommend another worflow?

I followed this process but is not working

import pandas as pd

# read csv data
path = r"C:\Users\Documents\"
df1 = pd.read_csv(path + '\obs_heads.csv')
df2 = pd.read_csv(path + '\sim.csv')

t = pd.merge_asof(df1, df2, on="A2")
print(t)

Input:

Data frame 1:

input1

Data frame 2:

input2

Output:

output

Error:

enter image description here Thanks,

1 Answers

I was seeing more posts here and I found the answer: Thanks to all the community

Joining Two Different Dataframes on Timestamp

Pandas date range returns "could not convert string to Timestamp" for yyyy-ww

import pandas as pd

# read csv data
path = r"C:\Users\1"
df1 = pd.read_csv(path + '\obs_heads2.csv')
df2 = pd.read_csv(path + '\HEAD_compiled_export.csv')

df1['Times'] = pd.to_datetime(df1['Times'])
df1=df1.set_index('Times')
df2['Times'] = pd.to_datetime(df2['Times'])
df2=df2.set_index('Times')

tol = pd.Timedelta('5 minute')
t=pd.merge_asof(left=df1,right=df2,right_index=True,left_index=True,direction='nearest',tolerance=tol)
t.to_csv( path +"\File Name.csv")
Related