在维基百科中,您可以找到一些有趣的数据进行排序,过滤,…
这是一个可爱的样本
{| class="wikitable sortable" |- ! Model !! Mhash/s !! Mhash/J !! Watts !! Clock !! SP !! Comment |- | ION || 1.8 || 0.067 || 27 || || 16 || poclbm; power consumption incl. cpu |- | 8200 mGPU || 1.2 || || || 1200 || 16 || 128 MB shared memory,"poclbm -w 128 -f 0" |- | 8400 GS || 2.3 || || || || || "poclbm -w 128" |- |}
我正在寻找一种将这些数据导入Python Pandas DataFrame的方法
解决方法
以下是使用
py-wikimarkup和
PyQuery从wikimarkup字符串中将所有表提取为熊猫DataFrames的解决方案,忽略非表内容.
import wikimarkup import pandas as pd from pyquery import PyQuery def get_tables(wiki): html = PyQuery(wikimarkup.parse(wiki)) frames = [] for table in html('table'): data = [[x.text.strip() for x in row] for row in table.getchildren()] df = pd.DataFrame(data[1:],columns=data[0]) frames.append(df) return frames
给出以下输入,
wiki = """ =Title= Description. {| class="wikitable sortable" |- ! Model !! Mhash/s !! Mhash/J !! Watts !! Clock !! SP !! Comment |- | ION || 1.8 || 0.067 || 27 || || 16 || poclbm; power consumption incl. cpu |- | 8200 mGPU || 1.2 || || || 1200 || 16 || 128 MB shared memory,"poclbm -w 128 -f 0" |- | 8400 GS || 2.3 || || || || || "poclbm -w 128" |- |} {| class="wikitable sortable" |- ! A !! B !! C |- | 0 | 1 | 2 |- | 3 | 4 | 5 |} """
get_tables返回以下DataFrames.
Model Mhash/s Mhash/J Watts Clock SP Comment 0 ION 1.8 0.067 27 16 poclbm; power consumption incl. cpu 1 8200 mGPU 1.2 1200 16 128 MB shared memory,"poclbm -w 128 -f 0" 2 8400 GS 2.3 "poclbm -w 128"
A B C 0 0 1 2 1 3 4 5