【发布时间】:2019-09-04 03:05:10
【问题描述】:
我使用 bs4 从 Wikipedia 收集体育数据。我能够提取两个列表:统计标题列表和统计信息。我想通过组合这两个列表来重新创建这个 df。 SO上有类似的帖子,但没有什么正是我需要的。 len(stat_header) = 13, len(stats) = 195
我能够创建字典,但在此过程中丢失了数据。 我尝试转换为数组,但由于形状的原因,仍然无法创建 df 或连接。我没说对。
stat_header = ['Year','Team','GP','GS','MPG','FG%','3P%','FT%',
'RPG',APG',SPG','BPG','PPG']
# Just a sample 14 out of 195
stats = ['1984-85','Chicago','82','82','38.3','.515','.173',
'.845','6.5', '5.9','2.4','.8','28.2','1985–86',
这可行,但我丢失了很多数据(大多数团队名称)
result = {v: stat_header[i % len(stat_header)]
for i, v in enumerate(stats)}
print("resultant dictionary : ", str(result))
重塑(不起作用,只是形状错误)
x = np.reshape(stats, (15, 13))
y = np.reshape(stat_header, (1, 13))
pd.DataFrame(x, columns=y)
And this...
np.concatenate( ( np.fromstring( y, dtype=np.uint8 ), x ), axis=0 )
我找到并提取了正确的信息。我想把它重新组合在一起,希望在一个 df 中。
【问题讨论】:
-
你研究过熊猫吗?熊猫有 pd.read_html() pandas.pydata.org/pandas-docs/stable/reference/api/…
标签: python python-3.x list dataframe beautifulsoup