[英]How to open the excel file creating from pandas faster?
從python創建的excel文件打開速度極慢,即使文件大小約為50 MB。
我試過了pandas和openpyxl。
def to_file(list_report,list_sheet,strip_columns,Name):
i = 0
wb = ExcelWriter(path_output + '\\' + Name + dateformat + '.xlsx')
while i <= len(list_report)-1:
try:
df = pd.DataFrame(pd.read_csv(path_input + '\\' + list_report[i] + reportdate + '.csv'))
for column in strip_column:
try:
df[column] = df[column].str.strip('=("")')
except:
pass
df = adjust_report(df,list_report[i])
df = df.apply(pd.to_numeric, errors ='ignore', downcast = 'integer')
df.to_excel(wb, sheet_name = list_sheet[i], index = False)
except:
print('Missing report: ' + list_report[i])
i += 1
wb.save()
反正有加速嗎?
讓我們將list_report
重命名為reports
。 那么你的while
循環通常表示為: for i in range(len(reports)):
您可以多次訪問第i
個元素。 循環可以為你綁定,對於: for i, report in enumerate(reports):
但事實證明你甚至不需要i
。 所以大多數人都會這樣寫: for report in reports:
這段代碼非常好:
for column in strip_column:
try:
df[column] = df[column].str.strip('=("")')
except:
pass
我建議你使用def strip_punctuation
將它埋沒在輔助函數中。 (列表應該是復數,我想? strip_columns
?)然后你會有一個簡單的df
賦值序列。
配置文件已用time()
。 使用以下代碼圍繞每個df
賦值:
t0 = time()
df = ...
print(time() - t0)
這將顯示您的處理管道的哪個部分耗時最長,因此應該盡最大努力加快它的速度。
我懷疑adjust_report()
使用了大部分時間,但沒有看到它很難說。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.