【问题标题】:Shaping pandas read_html results into simpler structure将 pandas read_html 塑造成更简单的结构
【发布时间】:2017-05-30 12:17:16
【问题描述】:

我希望有人可以建议我如何创建只包含第二列文本而不是第一两行或左列的 pandas 数据框。解决方案需要能够处理多个相似的表。

我曾认为pd.read_html(LOTable.prettify(),skiprows=2, flavor='bs4') 从 html 创建一个数据帧列表(跳过 2 行)是一种方式,但最终的数据结构对于这个新手来说太混乱了,无法理解或操作成更简单的结构。

其他人是否有办法使用产生的结构或推荐替代方法来优化数据,以便我最终得到 1 列只包含我需要的文本?

样本表

<table cellpadding="5" cellspacing="0" class="borders" width="100%">
    <tr>
     <th colspan="2">
      Learning Outcomes
     </th>
    </tr>
    <tr>
     <td class="info" colspan="2">
      On successful completion of this module the learner will be able to:
     </td>
    </tr>
    <tr>
     <td style="width:10%;">
      LO1
     </td>
     <td>
      Demonstrate an awareness of the important role of Financial Accounting information as an input into the decision making process.
     </td>
    </tr>
    <tr>
     <td style="width:10%;">
      LO2
     </td>
     <td>
      Display an understanding of the fundamental accounting concepts, principles and conventions that underpin the preparation of Financial statements.
     </td>
    </tr>
    <tr>
     <td style="width:10%;">
      LO3
     </td>
     <td>
      Understand the various formats in which  information in relation to transactions or events is recorded and classified.
     </td>
    </tr>
    <tr>
     <td style="width:10%;">
      LO4
     </td>
     <td>
      Apply a knowledge of accounting concepts,conventions and techniques such as double entry to the  posting of  recorded information to the T accounts in the Nominal Ledger.
     </td>
    </tr>
    <tr>
     <td style="width:10%;">
      LO5
     </td>
     <td>
      Prepare and present the financial statements of a Sole Trader  in prescribed format from a Trial Balance  accompanies by notes with additional information.
     </td>
    </tr>
   </table> 

【问题讨论】:

    标签: python pandas python-3.5 bs4


    【解决方案1】:

    第一个选项
    使用iloc

    这应该通过让iloc 摆脱第一列来工作`

    pd.read_html(LOTable.prettify(),skiprows=2, flavor='bs4').iloc[:, 1:]
    

    解释

    ...iloc[:, 1:]
    #       ^   ^
    #       |    \
    # says to    says to take columns
    # take all   starting with one and on
    # rows
    

    你可以只用一列

    pd.read_html(LOTable.prettify(),skiprows=2, flavor='bs4').iloc[:, 1]
    

    我运行的工作代码

    htm = """<table cellpadding="5" cellspacing="0" class="borders" width="100%">
        <tr>
         <th colspan="2">
          Learning Outcomes
         </th>
        </tr>
        <tr>
         <td class="info" colspan="2">
          On successful completion of this module the learner will be able to:
         </td>
        </tr>
        <tr>
         <td style="width:10%;">
          LO1
         </td>
         <td>
          Demonstrate an awareness of the important role of Financial Accounting information as an input into the decision making process.
         </td>
        </tr>
        <tr>
         <td style="width:10%;">
          LO2
         </td>
         <td>
          Display an understanding of the fundamental accounting concepts, principles and conventions that underpin the preparation of Financial statements.
         </td>
        </tr>
        <tr>
         <td style="width:10%;">
          LO3
         </td>
         <td>
          Understand the various formats in which  information in relation to transactions or events is recorded and classified.
         </td>
        </tr>
        <tr>
         <td style="width:10%;">
          LO4
         </td>
         <td>
          Apply a knowledge of accounting concepts,conventions and techniques such as double entry to the  posting of  recorded information to the T accounts in the Nominal Ledger.
         </td>
        </tr>
        <tr>
         <td style="width:10%;">
          LO5
         </td>
         <td>
          Prepare and present the financial statements of a Sole Trader  in prescribed format from a Trial Balance  accompanies by notes with additional information.
         </td>
        </tr>
       </table> """
    
    pd.read_html(htm,skiprows=2, flavor='bs4')[0].iloc[:, 1:]
    

    【讨论】:

    • 你能解释一下你的改进意味着什么吗?如果你需要将该代码添加到一个循环中,那么最后你有 1 列包含来自 5、34 或 106 个类似独立表的所有文本,这些表都开始了相同但可以有更少或更多的行?
    • 实际上我的问题的最后一点是处理循环,但在循环之前声明一个空数据帧InfoDF = pd.DataFrame() ,并将您的代码生成的数据帧连接到空数据帧给我我需要的结果InfoDF = pd.concat([InfoDF,pd.read_html(LOTable.prettify(),skiprows=2, flavor='bs4')[0].iloc[:, 1:]])跨度>
    猜你喜欢
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 2013-05-06
    • 2012-05-15
    • 2017-05-17
    相关资源
    最近更新 更多