如何使用pandas.Series.dt.strftime可以做到每小时映射?

问题描述 投票:1回答:1

这是我的数据:

 device_create_at               
136 2014-08-27 17:29:23            
245 2015-09-06 15:46:00            
257 2014-09-29 22:26:34            
258 2014-11-05 13:02:18    

这里是我的预期输出

  device_create_at                device_create_hour
136 2014-08-27 17:29:23            2014-08-27 17
245 2015-09-06 15:46:00            2015-09-06 15
257 2014-09-29 22:26:34            2014-09-29 22
258 2014-11-05 13:02:18            2014-11-05 13

据我所知,pandas.Series.dt.strftime能做到每周映射,代码是这样的

sheet2['device_create_week'] = sheet2['device_create_at'].dt.strftime('%Y-%V')

他们使用%V一周不%W,我尽量让这个小时

sheet2['device_create_hour'] = sheet2['device_create_at'].dt.strftime('%Y-%M-%D-%H')

这是不工作

python pandas dataframe timestamp series
1个回答
2
投票
s = df.device_create_at.dt.strftime('%Y-%m-%d %H')
print(s)
136    2014-08-27 17
245    2015-09-06 15
257    2014-09-29 22
258    2014-11-05 13
Name: device_create_at, dtype: object

需要注意的是格式%Y-%m-%d %Hm和小d

有关所有指令的详细信息,例如:%M,%M,您可以在the python documentation找到它。

© www.soinside.com 2019 - 2024. All rights reserved.