I'm trying to create a new series within a dataframe that maps a dictionary along two-dimensions, first matching the key, then matching the value within an array . The existing series is a datetime and the key match is against the date and the value match is the hour (thus the new series 'dh'
)
There is a similar question for mapping a 1-dimensional array here: Adding a new pandas column with mapped value from a dictionary, but that maps the entire array to each day.
Current code:
import pandas as pd
df = pd.DataFrame({'datetime':pd.date_range('1/1/2018','1/4/2018', freq = '1H', closed = 'left')})
day_hour = {1:range(48,0,-2),
2:range(96,0,-4),
3:range(120,0,-5) }
df['dh'] = df['datetime'].dt.day.map(day_hour)
Output snippet:
datetime dh
0 2018-01-01 00:00:00 [48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 2...
1 2018-01-01 01:00:00 [48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 2...
2 2018-01-01 02:00:00 [48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 2...
3 2018-01-01 03:00:00 [48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 2...
4 2018-01-01 04:00:00 [48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 2...
5 2018-01-01 05:00:00 [48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 2...
6 2018-01-01 06:00:00 [48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 2...
7 2018-01-01 07:00:00 [48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 2...
8 2018-01-01 08:00:00 [48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 2...
9 2018-01-01 09:00:00 [48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 2...
10 2018-01-01 10:00:00 [48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 2...
11 2018-01-01 11:00:00 [48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 2...
Desired Output:
datetime dh
0 2018-01-01 00:00:00 48
1 2018-01-01 01:00:00 46
2 2018-01-01 02:00:00 44
3 2018-01-01 03:00:00 42
4 2018-01-01 04:00:00 40
5 2018-01-01 05:00:00 38
...