我需要在数据帧中将TimeStamp条目转换为简单的dd mm yyyy格式,因为我需要在稍后阶段应用GroupBy。 问题是,格式根本不适用。我的代码是
from __future__ import division
import urllib.request
import csv
import pandas as pd
import datetime as dt
def changeFormat(inputFormat):
return pd.to_datetime(inputFormat, format='%Y%m%d')
df = pd.read_excel('Incidents.xlsx',encoding='ISO-8859-1')
df = df.sort_values('REGISTERED_DT#CST')
##df.info()
columnsToExt = ['REGISTERED_DT#CST', 'PRIORITY']
df1 = pd.DataFrame(df, columns=columnsToExt)
df1['REGISTERED_DT#CST'] = df1['REGISTERED_DT#CST'].apply(lambda x: changeFormat(x))
##by_date = df1.groupby('REGISTERED_DT#CST')
print(df1.head())
我的输出仍然与从未应用过的函数相同:
REGISTERED_DT#CST PRIORITY
0 2015-06-01 00:22:17 5
1 2015-06-01 06:42:27 4
2 2015-06-01 06:42:59 5
3 2015-06-01 07:00:03 5
4 2015-06-01 07:15:04 4
>>>
我希望它采用以下格式:
REGISTERED_DT#CST PRIORITY
0 2015-06-01 5
1 2015-06-01 4
2 2015-06-01 5
3 2015-06-01 5
4 2015-06-01 4
>>>