我现在有以下要摆脱的for循环,因为它太慢了。
for i in range(len(df)):
df.loc[i,"date_time"] = datetime.datetime.strptime(str(df.loc[i,"Date"] + ' ' + df.loc[i,"Time"]), '%d-%m-%Y %H:%M:%S')
输入:
index. date time
1 10-10-2010 00:00:00
2 10-10-2010 00:00:03
3 10-10-2010 00:00:06
4 10-10-2010 00:00:09
所需的输出,其中一列包含日期时间对象:
index. date time date_time
1 10-10-2010 00:00:00 10-10-2010 00:00:00
2 10-10-2010 00:00:03 10-10-2010 00:00:00
3 10-10-2010 00:00:06 10-10-2010 00:00:00
4 10-10-2010 00:00:09 10-10-2010 00:00:00
答案 0 :(得分:0)
请尽可能使用向量运算代替循环。这样的事情应该起作用(在包含的示例中进行了测试):
df['date_time'] = pd.to_datetime(df['date'] + ' ' + df['Time'])