gpt4 book ai didi

python - Python 中的 xlsxwriter 库突然变得很慢

转载 作者:行者123 更新时间:2023-12-05 07:17:43 25 4
gpt4 key购买 nike

我使用 xlsxwriter 库格式化 pandas 数据帧并将其写入 excel。该库是必需的,因为我需要各种格式(标题、表格等,因此我不使用 df.to_excel())。我遍历数据框的行和列来编写电子表格。

在过去的一年中,这在许多用途中都运行良好,包括大型数据帧(>10 万行,13 列)。然而,突然变得极其缓慢。它仍然有效,但现在需要几分钟左右的时间来编写(这似乎与 https://xlsxwriter.readthedocs.io/working_with_memory.html 处的 xlsxwriter 文档一致)现在需要很长时间才能编写(100 行需要 40 秒,所以 100k + 行需要永远) .我不知道有什么不同,因为代码本身是相同的并且仍然运行,只是速度很慢。我重新启动了我的机器,并更新了我所有的库,包括 xlsxwriter。我的其他不使用 xlsxwriter 的代码似乎都以正常速度运行,因此它似乎是特定于 xlsxwriter 的。我使用 Anaconda python 发行版。

像 xlswriter 这样的单一库会突然开始运行缓慢吗?我是在工作电脑上做的,所以也许他们改变了什么?我知道他们计划更新 Excel 版本,但还没有这样做。我就是想不通。

仅供引用,我的代码(之前以合理的速度运行)如下。

请注意,如果可以将 pandas 数据框写入 Excel,然后将其格式化为带有格式化标题行的表格,那么我会跳过循环,但我认为情况并非如此。

 # Populate data.
exceptions = exceptions.fillna('') # NA values not supported.
row = 3
i = 0
while row < len(exceptions) + 3:
n = 0
while n < len(column_formats):
col_name = column_formats[n][0]
col_format = column_formats[n][2]
if col_format == 'string':
exceptions[col_name].iloc[i] = str(exceptions[col_name].iloc[i])
worksheet.write_string(row, n, exceptions[col_name].iloc[i])
elif col_format == 'number':
if type(exceptions[col_name].iloc[i]) in [int, float, np.int32, np.float32, np.int64, np.float64]:
worksheet.write_number(row, n, exceptions[col_name].iloc[i],
number_format)
else:
worksheet.write_string(row, n, exceptions[col_name].iloc[i])
elif col_format == 'dollar':
if type(exceptions[col_name].iloc[i]) in [int, float, np.int32, np.float32, np.int64, np.float64]:
worksheet.write_number(row, n, exceptions[col_name].iloc[i],
dollar_format)
else:
worksheet.write_string(row, n, exceptions[col_name].iloc[i])
elif col_format == 'datetime':
# If date, then convert to python datetime and write to Excel.
if type(exceptions[col_name].iloc[i]) == pd.Timestamp:
date = exceptions[col_name].iloc[i].to_pydatetime()
worksheet.write_datetime(row, n, date,
date_format)
else:
worksheet.write_string(row, n, exceptions[col_name].iloc[i])
elif col_format == 'boolean':
if type(exceptions[col_name].iloc[i]) == bool:
worksheet.write_boolean(row, n, exceptions[col_name].iloc[i])
else:
worksheet.write_string(row, n, exceptions[col_name].iloc[i])
n +=1
row += 1
i += 1

最佳答案

好吧,我不确定为什么循环开始需要这么长时间,但我找到了一种方法将其写入 pandas 数据框,然后添加标题、格式化,然后将其制作成表格。 14 万行和 13 列只需不到一分钟的时间。

参见 https://xlsxwriter.readthedocs.io/pandas_examples.html了解详情。

以下是我如何将数据框制作成表格,以防有人感兴趣:

        if len(column_formats) + ex_cols <= 27:
col_string = string.ascii_uppercase[len(column_formats) + ex_cols]
else:
col_string = 'A' + string.ascii_uppercase[len(column_formats) + ex_cols - 27]

table_range = ('A3:' + col_string + str(len(exceptions) + 3))
col_headers = self._create_col_headers(column_formats, override_header)
worksheet.add_table(table_range, {'style': 'Table Style Medium 4',
'columns': col_headers})

def _create_col_headers(self, column_formats, override_header=None):
''''Creates a list of dictionaries of column headers, used to add table
format in Excel formatting'''
i = 0
col_headers = []
while i < len(column_formats):
col_name = column_formats[i][0]
col_headers.append({'header': col_name})
i += 1

# Add column for Review Notes (at end) (if no override).
if override_header is None:
col_headers.append({'header': 'Review Notes'})

return col_headers

关于python - Python 中的 xlsxwriter 库突然变得很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58648072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com