我试图在数据框中创建一个新系列,该系列沿二维方式映射字典,首先匹配键,然后匹配数组中的值。现有系列是日期时间,键匹配是日期,值匹配是小时(因此新系列'dh'
)
在这里映射一维数组有一个类似的问题:Adding a new pandas column with mapped value from a dictionary,但是将整个数组映射到每一天。
当前代码:
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)
输出摘录:
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...
期望的输出:
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
...
答案 0 :(得分:2)
如果您的dict创建得很好,则不需要地图
df['dh']=sum(map(list,day_hour.values()),[])
更新
df['dh'] = df['datetime'].dt.day.map(day_hour)
df['new']=df.groupby(df['datetime'].dt.date).cumcount()
df['dh']=df.apply(lambda x : x['dh'][x['new']],axis=1)
答案 1 :(得分:0)
我正在用你的问题打高尔夫球
sdk