import pandas as pd from dateutil.parser import parse #测试数据 test_dict = {0: {‘startTime‘: 20190825131028, ‘value‘: 1097}, 1: {‘startTime‘: 20190825132458, ‘value‘: 1083}, 2: {‘startTime‘: 20190825143034, ‘value‘: 1039}} df = pd.DataFrame(test_dict).T print(df) #定义函数后对整列apply def to_datetime(x): time_column = int(x) datetime_value = parse(str(time_column))#使用parse将时间字符转为datetime数据格式 return datetime_value df[‘startTime‘] = df[‘startTime‘].apply(lambda x:to_datetime(x)) print(df)
为什么要大费周章转为datetime格式?因为datetime更便于后续加工处理进行分析,例如datetime对象有如下方法:
[In] time1 = parse(‘20190825131028‘) print(time1) print(time1.day) print(time1.hour) [Out] 2019-08-25 13:10:28 25 13
原文地址:https://www.cnblogs.com/levylaw/p/12320642.html
时间: 2024-10-09 13:14:17