在这里,我使用 Selenium 抓取了 2021/22 至 2024 赛季的英超联赛表,并将其存储在
table_data
中,如下所示:
[['1',
'Arsenal',
'33 23 5 5 77 26 51 74',
'D',
'W',
'W',
'L',
'W',
'2',
'Liverpool',
'33 22 8 3 75 32 43 74',
'W',
'W',
'D',
'L',
'W',
'3',
'Manchester City',
'32 22 7 3 76 32 44 73',
'D',
'D',
'W',
'W',
'W',
'4',
'Aston Villa',
'34 20 6 8 71 50 21 66',
'W',
'L',
'D',
'W',
'W',
'5',
'Tottenham Hotspur',
'32 18 6 8 65 49 16 60',
'L',
'W',
'D',
'W',
'L',
'6',
'Newcastle United',
'32 15 5 12 69 52 17 50',
'L',
'W',
'D',
'W',
'W',
'7',
'Manchester United',
'32 15 5 12 47 48 -1 50',
'W',
'D',
'L',
'D',
'D',
'8',
'West Ham United',
'34 13 9 12 54 63 -9 48',
'L',
'D',
'W',
'L',
'L',
'9',
'Chelsea',
'31 13 8 10 61 52 9 47',
'W',
'D',
'W',
'D',
'W',
'10',
'Brighton And Hove Albion',
'32 11 11 10 52 50 2 44',
'W',
'L',
'D',
'L',
'D',
'11',
'Wolverhampton Wanderers',
'33 12 7 14 46 53 -7 43',
'L',
'D',
'L',
'D',
'L',
'12',
'Fulham',
'34 12 6 16 50 54 -4 42',
'D',
'L',
'L',
'W',
'L',
'13',
'Bournemouth',
'33 11 9 13 48 60 -12 42',
'W',
'W',
'L',
'D',
'L',
'14',
'Crystal Palace',
'33 9 9 15 42 56 -14 36',
'D',
'L',
'L',
'W',
'W',
'15',
'Brentford',
'34 9 8 17 52 59 -7 35',
'D',
'D',
'D',
'W',
'W',
'16',
'Everton',
'33 10 8 15 34 48 -14 30',
'L',
'D',
'W',
'L',
'W',
'17',
'Nottingham Forest',
'34 7 9 18 42 60 -18 26',
'D',
'W',
'L',
'D',
'L',
'18',
'Luton Town',
'34 6 7 21 47 75 -28 25',
'L',
'L',
'W',
'L',
'L',
'19',
'Burnley',
'34 5 8 21 37 69 -32 23',
'D',
'D',
'L',
'D',
'W',
'20',
'Sheffield United',
'33 3 7 23 31 88 -57 16',
'D',
'L',
'D',
'L',
'L'],
['1',
'Manchester City',
'38 28 5 5 94 33 61 89',
'W',
'W',
'W',
'D',
'L',
'2',
'Arsenal',
'38 26 6 6 88 43 45 84',
'W',
'W',
'L',
'L',
'W',
'3',
'Manchester United',
'38 23 6 9 58 43 15 75',
'L',
'W',
'W',
'W',
'W',
'4',
'Newcastle United',
'38 19 14 5 68 33 35 71',
'L',
'D',
'W',
'D',
'D',
'5',
'Liverpool',
'38 19 10 9 75 47 28 67',
'W',
'W',
'W',
'D',
'D',
'6',
'Brighton And Hove Albion',
'38 18 8 12 72 53 19 62',
'W',
'L',
'W',
'D',
'L',
'7',
'Aston Villa',
'38 18 7 13 51 46 5 61',
'L',
'L',
'W',
'D',
'W',
'8',
'Tottenham Hotspur',
'38 18 6 14 70 63 7 60',
'L',
'W',
'L',
'L',
'W',
'9',
'Brentford',
'38 15 14 9 58 46 12 59',
'W',
'L',
'W',
'W',
'W',
'10',
'Fulham',
'38 15 7 16 55 53 2 52',
'L',
'W',
'W',
'D',
'L',
'11',
'Crystal Palace',
'38 11 12 15 40 49 -9 45',
'W',
'L',
'W',
'D',
'D',
'12',
'Chelsea',
'38 11 11 16 38 47 -9 44',
'W',
'D',
'L',
'L',
'D',
'13',
'Wolverhampton Wanderers',
'38 11 8 19 31 58 -27 41',
'L',
'W',
'L',
'D',
'L',
'14',
'West Ham United',
'38 11 7 20 42 55 -13 40',
'L',
'W',
'L',
'W',
'L',
'15',
'Bournemouth',
'38 11 6 21 37 71 -34 39',
'W',
'L',
'L',
'L',
'L',
'16',
'Nottingham Forest',
'38 9 11 18 38 68 -30 38',
'L',
'W',
'D',
'W',
'D',
'17',
'Everton',
'38 8 12 18 34 57 -23 36',
'D',
'W',
'L',
'D',
'W',
'18',
'Leicester City',
'38 9 7 22 51 68 -17 34',
'D',
'L',
'L',
'D',
'W',
'19',
'Leeds United',
'38 7 10 21 48 78 -30 31',
'L',
'L',
'D',
'L',
'L',
'20',
'Southampton',
'38 6 7 25 36 73 -37 25',
'L',
'L',
'L',
'L',
'D'],
['1',
'Manchester City',
'38 29 6 3 99 26 73 93',
'W',
'W',
'W',
'D',
'W',
'2',
'Liverpool',
'38 28 8 2 94 26 68 92',
'W',
'D',
'W',
'W',
'W',
'3',
'Chelsea',
'38 21 11 6 76 33 43 74',
'L',
'D',
'W',
'D',
'W',
'4',
'Tottenham Hotspur',
'38 22 5 11 69 40 29 71',
'W',
'D',
'W',
'W',
'W',
'5',
'Arsenal',
'38 22 3 13 61 48 13 69',
'W',
'W',
'L',
'L',
'W',
'6',
'Manchester United',
'38 16 10 12 57 57 0 58',
'L',
'D',
'W',
'L',
'L',
'7',
'West Ham United',
'38 16 8 14 60 51 9 56',
'L',
'L',
'W',
'D',
'L',
'8',
'Leicester City',
'38 14 10 14 62 59 3 52',
'L',
'W',
'W',
'D',
'W',
'9',
'Brighton And Hove Albion',
'38 12 15 11 42 44 -2 51',
'D',
'W',
'W',
'D',
'W',
'10',
'Wolverhampton Wanderers',
'38 15 6 17 38 43 -5 51',
'L',
'D',
'L',
'D',
'L',
'11',
'Newcastle United',
'38 13 10 15 44 62 -18 49',
'W',
'L',
'L',
'W',
'W',
'12',
'Crystal Palace',
'38 11 15 12 50 46 4 48',
'W',
'W',
'D',
'L',
'W',
'13',
'Brentford',
'38 13 7 18 48 56 -8 46',
'D',
'L',
'W',
'W',
'L',
'14',
'Aston Villa',
'38 13 6 19 52 54 -2 45',
'W',
'L',
'D',
'D',
'L',
'15',
'Southampton',
'38 9 13 16 43 67 -24 40',
'D',
'L',
'L',
'L',
'L',
'16',
'Everton',
'38 11 6 21 43 66 -23 39',
'W',
'D',
'L',
'W',
'L',
'17',
'Leeds United',
'38 9 11 18 42 79 -37 38',
'L',
'L',
'L',
'D',
'W',
'18',
'Burnley',
'38 7 14 17 34 53 -19 35',
'W',
'L',
'L',
'D',
'L',
'19',
'Watford',
'38 6 5 27 34 77 -43 23',
'L',
'L',
'D',
'L',
'L',
'20',
'Norwich City',
'38 5 7 26 23 84 -61 22',
'L',
'L',
'L',
'D',
'L']]
我将其存储在 Dataframe 中,但是该 DataFrame 的输出看起来像这样 。它将每个季节存储为 1 行,而不是垂直向下,这就是它有 3 行和 160 列的原因。理想情况下,我希望它看起来就像 EPL 表一样 。
我尝试过转置,但它不起作用,我猜我需要自己映射行和列,但我对 pandas 很陌生,希望有人可以提供帮助。
如果您无法预先对其进行整形(刮擦时),这里是一些后处理的一种方法:
from itertools import chain, batched # >=3.12
df = (
pd.DataFrame(batched(chain.from_iterable(table_data), 8))
.pipe(
lambda raw: pd.concat(
[
raw.iloc[:, :2], # club's pos
raw.pop(2).str.split(expand=True), # game details
raw.iloc[:, 3:].agg(" ".join, axis=1), # form
],
axis=1,
)
)
.set_axis(
[
"Position", "Club", "Played",
"Won", "Draw", "Lost", "GF", "GA", "GD",
"Points", "Form",
],
axis=1,
)
)
NB:您可能需要添加一个标识符(此处:
1
、2
和3
)来区分排名时间。
输出:
Position Club Played Won Draw Lost GF GA GD Points Form
0 1 Arsenal 33 23 5 5 77 26 51 74 W W L W
1 2 Liverpool 33 22 8 3 75 32 43 74 W D L W
2 3 Manchester City 32 22 7 3 76 32 44 73 D W W W
3 4 Aston Villa 34 20 6 8 71 50 21 66 L D W W
.. ... ... ... .. ... ... .. .. ... ... ...
56 17 Leeds United 38 9 11 18 42 79 -37 38 L L D W
57 18 Burnley 38 7 14 17 34 53 -19 35 L L D L
58 19 Watford 38 6 5 27 34 77 -43 23 L D L L
59 20 Norwich City 38 5 7 26 23 84 -61 22 L L D L
[60 rows x 11 columns]