将 pandas 数据帧转换为单词表的更快/最快的方法

问题描述 投票:0回答:1

我正在尝试将 pandas 数据框转换为单词表。然而,对于大型数据帧,我当前使用的过程非常慢。这是因为每个单元格都必须被一一访问。据我所知,python-docx 中 table.cells 函数的调用使得代码如此缓慢

有没有办法做到这一点而不必单独调用每个单元格?或者是否有另一种更快的方法将 pandas 数据框转换为单词表?

def add_table(df):
  table = doc.add_table(df.shape[0]+1+(df.columns.nlevels -1), df.shape[1])
  table.style = 'Table Grid'
  #Add header rows for tables with more than 1 header
  if df.columns.nlevels > 1:
    for k in range(df.columns.nlevels):
      for j, cell in enumerate(table.rows[k].cells):
        cell.text = str(df.columns[j][k])


  else:
    # add the header rows.
    for j in range(df.shape[-1]):
        table.cell(0,j).text = df.columns[j]

  # add the rest of the dataframe
  for i in range(df.shape[0]): 
      for j, cell in enumerate(table.rows[i+1+(df.columns.nlevels -1)].cells): 
          cell.text = str(df.values[i, j])

输入数据:

                  Numb   Description
0                 301  DESC 1
1                 302  DESC 2
2                 303  DESC 3
3                 304  DESC 4
4                 305  DESC 5
...               ...                                                ...
2131             9108  DESC 6
2132             9109  DESC 7
2133             9110  DESC 8
2134             9111  DESC 9
2135             9112  DESC 10

预期输出:

麻木 描述
301 描述1
302 描述2
303 描述 3
304 描述 4
305 描述 5

编辑: 找到了一个很好的解决方案,它仅调用一次 table.cells 函数,然后迭代此单元格对象列表:https://github.com/python-openxml/python-docx/issues/174

python pandas dataframe python-docx
1个回答
0
投票

找到了一个很好的解决方案,仅调用一次 table.cells 函数,然后迭代此单元格对象列表:https://github.com/python-openxml/python-docx/issues/174

© www.soinside.com 2019 - 2024. All rights reserved.