我试图从HTML表的3列中获取3个字符串,但输出仍然是这样的:
空的DataFrame列:索引:[]空的DataFrame列:o索引:[]空的DataFrame列: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:[]‘。我只希望输出是:
0 o 1
只包含表中的值。
我如何阻止熊猫打印剩下的内容?网站上的表格是这样的:https://i.stack.imgur.com/6L909.png
发布于 2020-12-01 07:48:29
我运行了你的代码,这就是我得到的。您可以按如下格式很好地打印df[0]
:
from tabulate import tabulate
print(tabulate(df[0], headers = 'keys'))
它看起来就像这样。至少有两行。
('id4', 'AMHE') ('pid', '600959457') ('fc', '3871-4801-6097') ('host', '—') ('gid', '—') ('ls\u200b_\u200bstat', '0') ('ol\u200b_\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
不出所料。诸若此类。
https://stackoverflow.com/questions/65074746
复制相似问题