Python 3.x 转换;空数据帧";/将项目列表到Dataframe?
我使用Selenium(通过xpath)解析了一个网站上的表,然后在table元素上使用了Python 3.x 转换;空数据帧";/将项目列表到Dataframe?,python-3.x,pandas,Python 3.x,Pandas,我使用Selenium(通过xpath)解析了一个网站上的表,然后在table元素上使用了pd.read\u html,现在我只剩下一个组成表的列表。看起来是这样的: [Empty DataFrame Columns: [Symbol, Expiration, Strike, Last, Open, High, Low, Change, Volume] Index: [], Symbol Expiration Strike Last Open High Low Ch
pd.read\u html
,现在我只剩下一个组成表的列表。看起来是这样的:
[Empty DataFrame
Columns: [Symbol, Expiration, Strike, Last, Open, High, Low, Change, Volume]
Index: [], Symbol Expiration Strike Last Open High Low Change Volume
0 XPEV Dec20 12/18/2020 46.5 3.40 3.00 5.05 2.49 1.08 696.0
1 XPEV Dec20 12/18/2020 47.0 3.15 3.10 4.80 2.00 1.02 2359.0
2 XPEV Dec20 12/18/2020 47.5 2.80 2.67 4.50 1.89 0.91 2231.0
3 XPEV Dec20 12/18/2020 48.0 2.51 2.50 4.29 1.66 0.85 3887.0
4 XPEV Dec20 12/18/2020 48.5 2.22 2.34 3.80 1.51 0.72 2862.0
5 XPEV Dec20 12/18/2020 49.0 1.84 2.00 3.55 1.34 0.49 4382.0
6 XPEV Dec20 12/18/2020 50.0 1.36 1.76 3.10 1.02 0.30 14578.0
7 XPEV Dec20 12/18/2020 51.0 1.14 1.26 2.62 0.78 0.31 4429.0
8 XPEV Dec20 12/18/2020 52.0 0.85 0.95 2.20 0.62 0.19 2775.0
9 XPEV Dec20 12/18/2020 53.0 0.63 0.79 1.85 0.50 0.13 1542.0]
如何将其转换为实际的数据帧,以“Symbol、Expiration等…”作为标题,以最左边的列作为索引
我试过几种不同的方法,但都没用。我停下来的地方是:
# From reading the html of the table step
dfs = pd.read_html(table.get_attribute('outerHTML'))
dfs = pd.DataFrame(dfs)
。。。当我打印新的dfs时,我得到:
0 Empty DataFrame
Columns: [Symbol, Expiration, ...
1 Symbol Expiration Strike Last Open ...
根据文件
此函数将始终返回数据帧列表
,否则将失败,例如,它不会返回空列表
根据列表输出,非空数据框是该列表中的第二个元素。所以通过索引来检索它(记住Python使用零作为iterables的第一个索引)。请注意,您可以使用列表或目录中存储的数据帧
dfs[1]。head()
dfs[1].tail()
dfs[1]。描述()
...
single_df=dfs[1]。复制()
德尔菲斯酒店
或在同一呼叫上建立索引
single_df=pd.read_html(…)[1]
pd.read\u html
始终返回一个数据帧列表-索引到您想要/需要的数据帧上,这很有意义。谢谢你的回答!我学到了一些东西!