[Python] Flatten data from Pandas read_html but extracting the links

Discussão em 'Python' iniciado por Stack, Fevereiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have a html table like the following:

    html_text = "<table>
    <tr>
    <th>Home</th>
    <th>Score</th>
    <th>Away</th>
    <th>Report</th>
    </tr>
    <tr>
    <td>Arsenal</td>
    <td></td>
    <td>Manchester Utd</td>
    <td></td>
    </tr>
    <tr>
    <td>Everton</td>
    <td>2-0</td>
    <td>Liverpool</td>
    <td><a href="/asdasdasd/">Match Report</a></td>
    </tr>
    </table>"


    I'm loading it via requests and using Panda to convert to a dataset:

    matches = pd.read_html(StringIO(str(html_text)), extract_links="all")[0]


    This now gives me the following:

    (Home, None),(Score, None),(Away, None),(Report, None)
    (Arsenal, None),(NaN, None),(Manchester Utd, None),(NaN, None)
    (Everton, None),(2-0, None),(Liverpool, None),(Match Report, /asdasdasd/)


    What is the simplest way to flatten the dataset to (keeping only link in the "Report" column and the text elsewhere):

    Home, Score, Away, Report
    Arsenal, NaN, Manchester Utd, NaN
    Everton, 2-0, Liverpool, /asdasdasd/

    Continue reading...

Compartilhe esta Página