【发布时间】:2017-06-18 23:29:56
【问题描述】:
我正在使用 python notebook (jupyter) 运行一个辅助数据分析项目。数据集有约 1.3 行,我要做的第一件事是从数据集中的“日期”列中提取日、月和年。我写的代码执行得很好,只是它需要很长时间。我估计完成数据处理过程可能需要一个半小时。我想知道是否有人可以对我的代码提出一些建议以提高速度?
import csv
from datetime import datetime
def date_split(calendar):
new_calendar={}
i=0
calendar_total=pd.DataFrame()
num=calendar.shape[0]-1
while i<=10000:
tem=calendar_data.iloc[i,1]
#extract year&month&day from day column
listdate=datetime.strptime(tem,'%Y-%m-%d')
new_calendar['Year']=listdate.year
new_calendar['Month']=listdate.month
new_calendar['Date']=listdate.day
# add the other columns
new_calendar['listId']=calendar.iloc[i,0]
new_calendar['available']=calendar.iloc[i,2]
new_calendar['price']=calendar.iloc[i,3]
new_calendar=pd.DataFrame.from_records(new_calendar,index=[i])
#change new_calendar data type from dic to pd dataframe
calendar_total=calendar_total.append(new_calendar)
i=i+1
return calendar_total
同样,目标是从“日”列中提取年/月/日,并将它们制成新的数据框。在本地运行 python 中的代码也会显着加快速度吗?
谢谢
【问题讨论】:
-
您是否通过分析等发现了代码中的任何特定瓶颈?
标签: python algorithm pandas data-analysis data-science