2013-03-30 14 views
9

は、あなたが、フィルタ、ソートするいくつかの興味深いデータを見つけることができます...ここでWikipedia WikitableをPython Pandas DataFrameに変換するには?ウィキペディアで

は、私がこのようなデータをインポートする方法を探していますwikitable

{| 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

+0

:dictの:データフレームは、これらのいずれかから構成することができるhttp://pandas.pydata.org/pandas-docs/dev/dsintro.html#dataframe 1D配列、リスト、辞書、またはシリーズのうちの1つ; 2-D numpy.ndarray; 構造化またはレコードndarray。 Aシリーズ; 別のDataFrame。最も単純なのはlist/dictのdictですが、あなたのデータをこのように強制する方法は明白ではありません。あなたはどう思いますか? – hughdbrown

答えて

12

はここで抽出することpy-wikimarkupPyQueryを使用してソリューションですすべてのテーブルはwikimarkup文字列のpandas 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は、次のデータフレームを返します。これによれば

 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 
1

編集済み - 完全な回答は以下のとおりです。私はパンダをインストールしていないので、これがあなたのために働くかどうか教えてください。

from pandas import * 

wikitable = ''' 
{| 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" 
|- 
|}''' 
rows = wikitable.split('|-') 
header = [] 
table = [] 
for i in rows: 
    line = i.strip() 
    if line.startswith('!'): 
     header = line.split('!!') 
    elif line.startswith('|') and line.strip() != '|}': 
     table.append(line[2:].split('||')) 

data = {} 
for i in range(len(header) - 1): 
    col = [] 
    for row in table: 
     col.append(row[i]) 
    data[header[i]] = col 

print(data) 

df = DataFrame(data) 
+1

私はちょうどパンダのドキュメントを見ました(最初にやったはずです)。私に5分を与えると、私は完全な例を持っています。 – pycoder112358

2

使用reは、いくつかの前処理を行い、その後、DataFrameに変換するread_csvを使用する:

table = """{| 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" 
|- 
|}""" 

data = StringIO(re.sub("^\|.|^!.", "", table.replace("|-\n", ""), flags=re.MULTILINE)) 
df = pd.read_csv(data, delimiter="\|\||!!", skiprows=1) 

出力:

 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" 
関連する問題