1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| import pandas as pd
df1_na = pd.DataFrame([ ['LEAS', '03/06/2018 08:08'],
['LEAS', '03/06/2018 09:09'],
['LEAS', '03/06/2018 10:10'],
['LEAS', '03/06/2018 11:11']],
columns = ['origin_code', 'actual_time'])
df1_na['actual_time'] = pd.to_datetime(df1_na['actual_time'], format='%d/%m/%Y %H:%M')
df1_2 = pd.DataFrame([
['03/06/2018', 'LEAS', 9, 15.0, 100.0, 15.0, '03/06/2018 08:00'],
['03/06/2018', 'LEAS', 1, 14.0, 94.0, 15.0, '03/06/2018 08:30'],
['03/06/2018', 'LEAS', 18, 14.0, 94.0, 15.0, '03/06/2018 08:37'],
['03/06/2018', 'LEAS', 47, 14.0, 100.0, 14.0, '03/06/2018 08:52'],
['03/06/2018', 'LEAS', 19, 14.0, 100.0, 14.0, '03/06/2018 09:00'],
['03/06/2018', 'LEAS', 16, 14.0, 100.0, 14.0, '03/06/2018 09:09'],
['03/06/2018', 'LEAS', 48, 14.0, 100.0, 14.0, '03/06/2018 09:30'],
['03/06/2018', 'LEAS', 7, 13.0, 94.0, 14.0, '03/06/2018 10:00'],
['03/06/2018', 'LEAS', 14, 13.0, 94.0, 14.0, '03/06/2018 10:23'],
['03/06/2018', 'LEAS', 17, 13.0, 94.0, 14.0, '03/06/2018 10:30'],
['03/06/2018', 'LEAS', 35, 13.0, 94.0, 14.0, '03/06/2018 10:47'],
['03/06/2018', 'LEAS', 33, 13.0, 94.0, 14.0, '03/06/2018 11:00'],
], columns = ['day', 'station', 'daily_report', 'dew_point', 'rel_humidity', 'temp_c', 'time'])
df1_2['day'] = pd.to_datetime(df1_2['day'], format='%d/%m/%Y')
df1_2['time'] = pd.to_datetime(df1_2['time'], format='%d/%m/%Y %H:%M')
def f(x):
return df1_na.loc[(df1_na[(df1_na['origin_code'] == x['station'])]['actual_time'] - x['time']).abs().idxmin()]['actual_time']
df1_2['closest_time'] = df1_2.apply(f, axis=1) |
Partager