我试图从HTML表中的3列中获取3个字符串,但输出一直是这样的:
空数据帧列:[0]索引:[]空数据帧列:[o]索引:[]空数据帧列:[1]索引:[]
这是我的代码:
import pandas as pd
df = pd.read_html('http://wiimmfi.de/stats/game/mprimeds', skiprows = [0], encoding = 'utf-8')
df[0].columns
ls_stat = df[0]["ls_stat"].to_string(index = False)
ol_stat = df[0]["ol_stat"].to_string(index = False)
status = df[0]["status"].to_string(index = False)
print(ls_stat)
print(ol_stat)
print(status)
尽管表的第0行中有值,但它仍然会为每个字符串打印"Empty DataFrame Columns:[]index:[]"。我只想输出:
0o1
只包含表中的值。
如何阻止熊猫打印其余部分?网站上的表格如下所示:https://i.stack.imgur.com/6L909.png
我运行了您的代码,这就是我得到的。您可以打印格式良好的df[0]
:
from tabulate import tabulate
print(tabulate(df[0], headers = 'keys'))
它会是这样的。至少有两排。
('id4', 'AMHE') ('pid', '600959457') ('fc', '3871-4801-6097') ('host', '—') ('gid', '—') ('lsu200b_u200bstat', '0') ('olu200b_u200bstat', 'og') ('status', '2') ('suspend', '—') ('n', '1') ('name1', 'りー') ('name2', '—')
-- ----------------- ---------------------- -------------------------- --------------- -------------- ------------------------------ ------------------------------- ----------------- ------------------ ------------ ------------------- ----------------
0 AMHE 601081334 5159-9715-6854 — — 0 o 6 — 1 ュoco×èno™ —
1 AMHE 601087019 0564-3566-1867 — — 0 og 2 — 1 atrueboss —
我们还注意到,列是一个多索引——每个标签都是一个元组,而不是一个简单的名称。我们可以通过打印df[0].columns
(您已经在代码中这样做了(进行双重检查:
MultiIndex([( 'id4', 'AMHE'),
( 'pid', '600959457'),
( 'fc', '3871-4801-6097'),
( 'host', '—'),
( 'gid', '—'),
('ls_stat', '0'),
('ol_stat', 'og'),
( 'status', '2'),
( 'suspend', '—'),
( 'n', '1'),
( 'name1', 'りー'),
( 'name2', '—')],
)
因此,为了获得您想要的第一列,我们需要(注意上面从df[0].columns
中计算出的索引('ls_stat','0')
(
print(df[0][('ls_stat','0')].to_string(index = False))
所以我们得到
0
0
正如预期的那样。
对于ol_stat
,我们使用相同的技巧
print(df[0][('ol_stat','og')].to_string(index = False))
我们得到
o
og
正如预期的那样。等等。