需要关于加速python代码数据清理的建议吗

2024-04-26 05:10:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python笔记本(jupyter)运行一个side数据分析项目。数据集有~1.3行,我要做的第一件事是从数据集中的'date'列中提取day、month和year。我写的代码执行得很好,只是需要很长时间。我估计完成数据处理程序可能需要一个半小时。我想知道是否有人能对我的代码提出一些建议来提高速度?你知道吗

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本地运行代码是否也能显著加快速度?你知道吗

谢谢


Tags: 数据代码fromimportnewdatetimedateyear
1条回答
网友
1楼 · 发布于 2024-04-26 05:10:59

这就是我如何将年、月和日从现有数据帧提取到新数据帧中的方法:

import numpy as np
import pandas as pd

df = pd.DataFrame({'date' : pd.date_range("19970202", periods=365*20)})

df2 = pd.DataFrame({'year' : df['date'].dt.year, 'month' : df['date'].dt.month, 'day' : df['date'].dt.day})

print (df)
print (df2)

我还没有对一个大的数据集(130万行?),但也许这能给你提速。你知道吗

相关问题 更多 >