简体   繁体   中英

Merging Pandas Dataframe by Datetime and Date column

I have two Dataframes looking the following.

timestamp                 data
2015-01-01 00:00:00       296.0
2015-01-01 00:30:00       342.0
2015-01-01 01:00:00       431.0
2015-01-01 01:30:00       234.0
2015-01-01 02:00:00       234.0
...
2015-02-01 00:00:00       123.0
...

and

date             different date
2015-01-01       111
2015-01-02       233
2015-01-03       1324
2015-01-04       1231
2015-01-05       112
...

What I want is

timestamp                 data     different date
2015-01-01 00:00:00       296.0    111
2015-01-01 00:30:00       342.0    111
2015-01-01 01:00:00       431.0    111
2015-01-01 01:30:00       234.0    111
2015-01-01 02:00:00       234.0    111
...
2015-02-01 00:00:00       123.0    233
...

So what I want is a merge from one dataframe to the other. Where If the date is the same, every datetime row that fits the day gets the other value. Unfortunately my secound dataframe (the one with one value per day) has missing rows, so I cant just expand every value 48 times. Any help is appreciated. Looping over both dataframes and comparing the date seems very inefficient.

You can create helper column filled by dates from datetimes in df1 and then merge by it with left join by DataFrame.merge , last if necessary remove this column:

df1['timestamp'] = pd.to_datetime(df1['timestamp'])
df2['date'] = pd.to_datetime(df2['date']).dt.date

df1['date'] = df1['timestamp'].dt.date

df = df1.merge(df2, on='date', how='left').drop('date', axis=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM