- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 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/
我有一个 view我拖了一个UITableView在里面,还有 2 UIImageView s(第一个显示背景图像,第二个只是在 View 顶部显示一个非常小的标题和图像)。 它们都设置为 weak特
我尝试用 C# 编写简单的 PostgreSQL 查询。第一个 connection.open() 需要 20 秒。其他连接立即执行。 PGAdmin 工作也很慢。如果我打开“查看所有行”,它也需要大
我制作了一个 html5 视频播放器,我注意到如果当前播放的视频有点大,搜索时间会异常地长。 越接近终点,寻找的时间越长;独立于我之前是否去过那里/与当前时间点的距离有多近,或者我是否缓冲了整个视频。
我正在使用 MaterialDatePicker,但速度很慢。 public class MainActivity extends AppCompatActivity { MaterialDa
我想知道为什么 MyBatis 是 慢 在我的应用程序中。 对于 SELECT COUNT(*) ,所用时间为: 20 秒 - 第一个请求 2-3 秒 - 后续请求 缓存很可能使后续请求更快。 配置
我已经安装了一个默认的开箱即用的 FreeSwitch 实例,但是当我尝试进行内部调用(分机到分机)时,大约需要 12 秒才能建立调用并且我可以听到铃声。 当我查看日志时,我几乎立即看到了连接请求,但
我已经放弃了让它跑得更快的实际尝试。 我最大的问题是,当我插入 html 时,应用程序会变慢到爬行。我有一个进度条,我正在调用 QCoreApplication.processEvents() (顺便
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
Doxygen 在我们的代码库上运行大约需要 12 个小时。这主要是因为有很多代码要处理(约 1.5M 行)。然而,它很快就会接近我们无法进行夜间文档更新的地步,因为它们需要太长时间。我们已经不得不减
我正在重写我的旧渲染管道。我根据自己的喜好创建了一个非常精简的原型(prototype),令我震惊的是,我原来相当复杂且优化不佳的管道与 super 简单的原型(prototype)具有完全相同的性能
我想为我的网站使用 Gridster,但我需要使用“add_widget”命令添加很多小部件。我做了一个测试,我认为“add_widget”功能存在问题:网格越来越慢并且存在内存泄漏。 您可以在此视频
我有一份包含图表和表格的报告。 我正在使用 html2canvas与 jsPDF将此报告导出为 PDF 文件。 但是这个过程耗时很长,超过11000ms。 我尝试更改格式和质量,但没有任何效果。 请看
我正在查询大于时间戳的类的所有修订,使用: AuditReaderFactory .get(emf.createEntityManager()) .createQuery().forR
我最近想加速一个加密系统。而在这个系统中,它将使用mysql,因此它包括文件。 而且我发现系统运行缓慢并不是因为加解密,而是因为处理一些sql语句。 它将在运行时使用内存数据库,并使用 中的 mys
谁能看出为什么这需要大约 20 秒?我正在运行下面的代码以将 JSON 请求发布到本地服务器 192.168.1.127。 curl -H "Content-type: application/jso
我有两个表:Posts 和Tags,其中存储了用户发布的文章以及他们为文章附加的标签。 PostTags 表用于表示文章 ID 和标签 ID 的关系。结构如下: 帖子: id | title | au
一个我应该能够自己回答但我没有,而且在谷歌中也找不到任何答案的问题: 我有一个表,其中包含具有以下结构的 500 万行: CREATE TABLE IF NOT EXISTS `files_histo
以下查询在具有大约 50 万行的表上执行需要 20 多秒: SELECT images.id, images.user_id, images_locale.filename, extension, s
我正在使用 $.getJSON 来提取对象 list (100 个项目,不是一个大集合),但 XHR 调用需要 8-10 秒。 想了解我是否缺少某些内容或我可以采取哪些措施来加快我的计划? 最佳答案
在这段代码中,我从网站获取一个字符串并将其显示在标签上。在标签上显示字符串真的很慢!大约 10 秒。但是在控制台 println (date) 上打印字符串时是立即的。我该如何解决这个问题?
我是一名优秀的程序员,十分优秀!