美好的一天
我有一个包含3列的pandas数据帧。第二列是日期格式yyyy-mm-dd,但我想将这些转换为刻度。我想知道最有效的方法(即我不想遍历每一行)。
我的代码如下:
#Example of getting ticks
import time; # This is required to include time module.
ticks = time.time()
print("Number of ticks since 12:00am, January 1, 1970:", ticks)
#Example of converting to ticks
import datetime
s = "01/12/2015"
time.mktime(datetime.datetime.strptime(s, "%d/%m/%Y").timetuple())
#My code
import numpy as np
import pandas as pd
path = "C:/Users/geoff/OneDrive - Noble Brothers Consult (Pty) Ltd/Clients/GrAM/Daily Price Data/"
filename = "SST_5_Sept_2016_formatted.csv"
full_path = path + filename
data = pd.read_csv(full_path)
#here i need to convert the column data['Date'] to ticks
我上传了一个截图我的数据文件:
数据文件截图:
非常感谢你的帮助。
答案 0 :(得分:0)
首先,将转换为刻度的部分包装在函数中,例如
def convert_to_tic(s):
return time.mktime(datetime.datetime.strptime(s, "%d/%m/%Y").timetuple())
然后,将您的专栏变为DataFrame
并使用apply。类似this answer