python运行时间太长_Python脚本运行时间太长?

我正在编写一个python脚本,它基本上执行以下操作将CSV文件作为数据帧对象读取。在

根据名称选择一些列并将它们存储在新的DF对象中。在

对单元格中的值进行一些数学和字符串操作。我在这里使用for循环和iterrows()方法。在

将修改后的数据框写入CSV

使用for循环将CSV写入json。在

这段代码要花很长时间才能运行。我想知道为什么要花这么长时间,以及我是否应该以不同的方式来完成任务,以加快执行速度。在import pandas

import json

import pendulum

import csv

import os

import time

start_time = time.time()

print("--- %s seconds ---" % (time.time() - start_time))

os.chdir('/home/csv_files_from_REC')

df11 = pandas.read_csv('RTP_Gap_2018-01-21.csv') ### Reads the CSV FILE

print df11.shape ### Prints the shape of the DF

### Filter the initial DF by selecting some columns based on NAME

df1 = df11[['ENODEB','DAY','HR','SITE','RTP_Gap_Length_Total_sec','RTP_Session_Duration_Total_sec','RTP_Gap_Duration_Ratio_Avg%']]

print df1.shape ## Prints Shape

#### Math and String manupulation stuff ###

for index, row in df1.iterrows():

if row['DAY'] == 'Total':

df1.drop(index, inplace=True)

else:

stamp = row['DAY'] + ' ' + str(row['HR']) + ':00:00'

sitename = str(row['ENODEB'])+'_'+row['SITE']

if row['RTP_Session_Duration_Total_sec'] == 0:

rtp_gap = 0

else:

rtp_gap = row['RTP_Gap_Length_Total_sec']/row['RTP_Session_Duration_Total_sec']

time1 = pendulum.parse(stamp,tz='America/Chicago').isoformat()

df1.loc[index,'DAY'] = time1

df1.loc[index,'SITE'] = sitename

df1.loc[index,'HR'] = rtp_gap

### Write DF to CSV ###

df1.to_csv('RTP_json.csv',index=None)

json_file_ind = 'RTP_json.json'

file = open(json_file_ind, 'w')

file.write("")

file.close()

#### Write CSV to JSON ###

with open('RTP_json.csv', 'r') as csvfile:

reader_ind = csv.DictReader(csvfile)

row=[]

for row in reader_ind:

row["RTP_Gap_Length_Total_sec"] = float(row["RTP_Gap_Length_Total_sec"])

row["RTP_Session_Duration_Total_sec"] = float(row["RTP_Session_Duration_Total_sec"])

row["RTP_Gap_Duration_Ratio_Avg%"]=float(row["RTP_Gap_Duration_Ratio_Avg%"])

row["HR"] = float(row["HR"])

with open('RTP_json.json', 'a') as json_file_ind:

json.dump(row, json_file_ind)

json_file_ind.write('\n')

end_time = time.time()

print("--- %s seconds ---" % (time.time() - end_time))

输出

^{pr2}$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值