Home > Software engineering >  merging two dataframe using datetime index
merging two dataframe using datetime index

Time:10-10

I have two data frames as blows (df5 and dfS), saving air pollution data. Their index is DateTime, as can been seen. In the DateTime index, the second reading is different. So I want to merge them when a date, hour and Minutes of their reading is the same and ignore the second. df5:

enter image description here

dfS:

enter image description here

Below is my code:

dfG=df5.merge(dfS,left_on='Min', right_on='Min' )
dfG['DATETIME'] = dfG.Date   "_"   dfG.Time
dfG.to_csv('dfG.csv')
dfG.set_index("DATETIME", inplace=True)
#correct the underscores in old datetime format
dfG.index = [" ".join(str(val).split("_")) for val in dfG.index]

as it can be seen the index is not correct because I considered 'Min' as key for joining.

DfG: enter image description here

My question is: How to merge these two data frames based on DateTime index ignoring the second.

CodePudding user response:

if the seconds on your timestamp is not important , maybe this can help:

df5.index = df5.index.map(lambda x: x.replace(second=0))
  • Related