我正在做一个从Cisco ISE中提取数据的小项目。原始数据的属性比我所需要的要多得多。因此,我将文件提取为更少的属性,下面是代码。
from __future__ import print_function, unicode_literals
import csv
import pandas as pd
data = pd.read_csv('profiler_endpoints.csv')
df = pd.DataFrame(data)
df = df[["MACAddress","ip","host-name","operating-system","UpdateTime"]]
df['UpdateTime'] = pd.to_datetime(df['UpdateTime'])
df.to_excel("profiler_endpoints_trimmed3.xlsx")提取的表位于上面的链接处。我在使用日期和时间过滤数据时遇到了问题,因为列中UpdateTime的值为2021-04-15 11:46:44+0800格式,并且当我试图用下面的代码将值转换为日期格式时,错误的11:46:44+0800日期超出了月份的范围: 0。
df['UpdateTime'] = pd.to_datetime(df['UpdateTime'])
我是否有任何方法将该值转换为2021-04-15 11:46:44+0800或将值从2021-04-15转换为2021-04-15,所以我相信使用pd.to_datetime()没有问题。
发布于 2021-06-14 04:16:18
# First make the dataframe (just the time column)
data = {'UpdateTime': [
'2020-12-16 01:10:09+0800',
'2020-12-16 01:10:09+0800',
'2020-05-28 01:56:56+0800',
'2020-09-27 09:47:42+0800',
'2020-05-28 01:56:56+0800',
'2020-02-18 10:01:56+0800',
]}
df = pd.DataFrame(data)
# now convert to datetime
df['UpdateTime']=pd.to_datetime(df['UpdateTime'].str.split(' ',1).str[0])
# now double check that in fact we have a datetime
df.info()
out:
RangeIndex: 6 entries, 0 to 5
Data columns (total 1 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 UpdateTime 6 non-null datetime64[ns]
dtypes: datetime64[ns](1)注意,上面的dtypes是datetime64。你完蛋了!
https://stackoverflow.com/questions/67964630
复制相似问题