我是python 3的新手我必须从.csv文件中获取日期时间值并将其拆分为单独的日期和时间值列。
csv文件:
C1,C2,V2,Time_Stamp
0.1,1.3,-0.9,13/6/2017 14:42
0.1,1.2,-0.9,13/6/2017 14:42
0.1,1.3,-0.9,13/6/2017 14:42
0.1,1.3,-0.9,13/6/2017 14:42
最初,我在Time_Stamp下有这个:
运行后输出:
timestamps = Data.Time_Stamp
for i in timestamps:
dataobj = dt.datetime.strptime(i, "%d/%m/%Y %H:%M") #Time_Stamp datetime format - need set varibale to print
print (dataobj) #Prints the following output
输出:
13/6/2017 14:42
13/6/2017 14:42
13/6/2017 14:42
13/6/2017 14:42
13/6/2017 14:42
根据上面的输出,它在一列中有日期和时间,我希望日期在不同的列中,时间在另一列中。
13/6/2017 14:42 13/6/2017 14:42
13/6/2017 14:42 13/6/2017 14:42
13/6/2017 14:42 13/6/2017 14:42
13/6/2017 14:42 13/6/2017 14:42
13/6/2017 14:42 13/6/2017 14:42
我尝试了以下Splitting timestamp column into seperate date and time columns中的示例,但我无法获得类似的输出。
import pandas as pd
import csv
import datetime as dt
print (row)
Data = pd.DataFrame.from_csv('csv_date.csv')
print (Data)
timestamps = Data.Time_Stamp
for i in timestamps:
dataobj = datetime.strptime(i, "%d/%m/%Y %H:%M")
df['new_date'] = [i.date() for i in Data['Time_Stamp']]
df['new_time'] = [i.time() for i in Data['Time_Stamp']]
我遇到的错误:
AttributeError Traceback (most recent call last)
<ipython-input-137-ca9ed5c25ff2> in <module>()
----> 1 df['new_date'] = [i.date() for i in Data['Time_Stamp']]
2 df['new_time'] = [i.time() for i in Data['Time_Stamp']]
<ipython-input-137-ca9ed5c25ff2> in <listcomp>(.0)
----> 1 df['new_date'] = [i.date() for i in Data['Time_Stamp']]
2 df['new_time'] = [i.time() for i in Data['Time_Stamp']]
AttributeError: 'str' object has no attribute 'date'
答案 0 :(得分:1)
阅读时,将pd.read_csv
与parse_dates
和infer_datetime_format
一起用作True
。
Data = pd.read_csv('csv_date.csv', parse_dates=['Time_Stamp'], infer_datetime_format=True)
接下来,您只需提取日期&amp;时间:
Data['Date'] = Data.Time_Stamp.dt.date
Data['Time'] = Data.Time_Stamp.dt.time
答案 1 :(得分:0)
Data = pd.DataFrame.from_csv('csv_date.csv')
Data['new_times'] = Data['Time_Stamp'].apply(lambda x:datetime.strptime(x, "%d/%m/%Y %H:%M"))
使用apply和lambda处理Dataframe