代码之家  ›  专栏  ›  技术社区  ›  spoikayi

如何删除数据帧标题并使其成为一行

  •  0
  • spoikayi  · 技术社区  · 2 年前

    tables = tabula.read_pdf(file, pages="all")
    

    这很好用。现在tables是一个数据帧列表,其中每个数据帧都是pdf文件中的一个表。

    当前数据帧:

      Component manufacturer               DMNS
    0         Component name               KL32/OOH8
    1         Component type               LTE-M/NB-IoT
    2       Package markings               <pin 1 marker>\ ksdc 99cdjh
    3              Date code               Not discerned
    4           Package type               127-pin land grid array (LGA)
    5           Package size               26.00 mm × 10.11 mm × 3.05 mm
    

            0                                1
    0       Component manufacturer           DMNS
    1       Component name                   KL32/OOH8
    2       Component type                   LTE-M/NB-IoT
    3       Package markings                 <pin 1 marker>\ ksdc e99cdjh
    4       Date code                        Not discerned
    5       Package type                     127-pin land grid array (LGA)
    6       Package size                     26.00 mm × 10.11 mm × 3.05 mm
    

    我该如何进行这种转换?

    2 回复  |  直到 2 年前
        1
  •  2
  •   fsimonjetz    2 年前

    作为 tabula docs on read_pdf 状态,您可以添加 pandas_options {'header': None} .所以(类似的)这应该可以做到:

    tabula.read_pdf(file, pages="all", pandas_options={'header': None})
    

    multiple_tables False 这不是默认值。我会稍微考虑一下选项,如果没有达到预期效果, here

        2
  •  1
  •   constantstranger    2 年前

    下面是一种解决问题的方法:

    df = df.T.reset_index().T.reset_index(drop=True)
    

                            0                              1
    0  Component manufacturer                           DMNS
    1          Component name                      KL32/OOH8
    2          Component type                   LTE-M/NB-IoT
    3        Package markings    <pin 1 marker>\ ksdc 99cdjh
    4               Date code                  Not discerned
    5            Package type  127-pin land grid array (LGA)
    6            Package size  26.00 mm × 10.11 mm × 3.05 mm
    

    说明:

    • reset_index() 将索引(即原始列标签)转换为新的初始列
    • 再次将其转置,使新的初始列成为初始行,并使用 获取新的整数索引。