如何通过将许多数据帧附加在一起创建一个主数据帧?

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

我试图循环处理多页格式相同的数据,以获得一个由所有数据拼接而成的大型主数据框。然而,当我运行这段代码时,master_df是空的,好像new_df没有被追加到主数据框中。有什么想法吗?先谢谢你

alphabet_list = ['a','b','c','d','e','f']

master_df = pd.DataFrame([])

for letter in alphabet_list:

    player_df = pd.read_html('https://www.nfl.com/players/active/{}'.format(letter))

    new_df = player_df[0]

    master_df = new_df.append(new_df)
pandas loops dataframe web-scraping append
1个回答
0
投票

试试这个改动。

import pandas as pd

alphabet_list = ['a','b','c','d','e','f']

master_df = pd.DataFrame()

for letter in alphabet_list:

    player_df = pd.read_html('https://www.nfl.com/players/active/{}'.format(letter))

    master_df = master_df.append(player_df)

输出:

                Player          Current Team Position Status
0       Chidobe Awuzie        Dallas Cowboys       CB    ACT
1           Josh Avery      Seattle Seahawks       DT    ACT
2         Genard Avery   Philadelphia Eagles      OLB    ACT
3      Anthony Averett      Baltimore Ravens       CB    ACT
4            Lee Autry         Chicago Bears       DT    ACT
5         Denico Autry    Indianapolis Colts       DT    ACT
6         Tavon Austin        Dallas Cowboys       WR    UFA
7      Blessuan Austin         New York Jets       DB    ACT
8       Antony Auclair  Tampa Bay Buccaneers       TE    ACT
9    Jeremiah Attaochu        Denver Broncos       LB    ACT
10     Hunter Atkinson       Atlanta Falcons       OT    ACT
11         John Atkins         Detroit Lions       DE    ACT
12         Geno Atkins    Cincinnati Bengals       DT    ACT
13      Marcell Ateman     Las Vegas Raiders       WR    ACT
14        George Aston       New York Giants       RB    ACT
15  Dravon Askew-Henry       New York Giants       DB    ACT
16        Devin Asiasi  New England Patriots       TE    ACT
17  George Asafo-Adjei       New York Giants       OT    ACT
18           Ade Aruna     Las Vegas Raiders       DE    ACT
19     Grayland Arnold   Philadelphia Eagles      SAF    ACT
20          Dan Arnold     Arizona Cardinals       TE    ACT
21       Damon Arnette     Las Vegas Raiders       CB    UDF
22   Ray-Ray Armstrong        Dallas Cowboys       LB    UFA
23   Ka'John Armstrong        Denver Broncos       OT    ACT
24   Dorance Armstrong        Dallas Cowboys       DE    ACT
25   Cornell Armstrong        Houston Texans       DB    ACT
26     Terron Armstead    New Orleans Saints       OT    ACT
27    Ryquell Armstead  Jacksonville Jaguars       RB    ACT
28       Arik Armstead   San Francisco 49ers       DE    ACT
29          Alex Armah     Carolina Panthers       FB    ACT
..                 ...                   ...      ...    ...
70         Mark Fields     Minnesota Vikings       CB    ACT
71          Sam Ficken         New York Jets        K    ACT
72        Jason Ferris     Carolina Panthers       LB    ACT
73      Clelin Ferrell     Las Vegas Raiders       DE    ACT
74       Reid Ferguson         Buffalo Bills       LS    ACT
75       Josh Ferguson   Washington Redskins       RB    ACT
76     Jaylon Ferguson      Baltimore Ravens       LB    ACT
77      Blake Ferguson        Miami Dolphins       LS    ACT
78       James Ferentz  New England Patriots        C    UFA
79       Rashad Fenton    Kansas City Chiefs       DB    ACT
80        Darren Fells        Houston Texans       TE    UFA
81       Jon Feliciano         Buffalo Bills       OG    ACT
82   Clayton Fejedelem        Miami Dolphins       DB    ACT
83         Matt Feiler   Pittsburgh Steelers       OT    RFA
84         Jordan Fehr     Minnesota Vikings       LB    ACT
85      Breiden Fehoko  Los Angeles Chargers       DT    ACT
86          Dan Feeney  Los Angeles Chargers       OG    ACT
87      Tavien Feaster  Jacksonville Jaguars       RB    ACT
88      Foley Fatukasi         New York Jets       DT    ACT
89  Rojesterman Farris       Atlanta Falcons       DB    ACT
90   Wesley Farnsworth        Denver Broncos       LS    ACT
91     Matthias Farley         New York Jets       DB    ACT
92           Noah Fant        Denver Broncos       TE    ACT
93         George Fant         New York Jets       OT    ACT
94         David Fales         New York Jets       QB    ACT
95          Nico Falah        Denver Broncos        C    ACT
96    Ka'imi Fairbairn        Houston Texans        K    ACT
97         Jovahn Fair    Kansas City Chiefs       OG    ACT
98     Brandon Facyson  Los Angeles Chargers       DB    ACT
99      Kyler Fackrell       New York Giants       LB    ACT

[571 rows x 4 columns]
© www.soinside.com 2019 - 2024. All rights reserved.