2013-03-30 14 views
9

In Wikipedia, è possibile trovare alcuni dati interessanti da ordinare, filtrare, ...Come convertire Wikipedia wikitable in Python Pandora DataFrame?

Ecco un esempio di un 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" 
|- 
|} 

Sto cercando un modo per importare tali dati a Python Pandas DataFrame

+0

Secondo questo: http://pandas.pydata.org/pandas-docs/dev/dsintro.html#dataframe un dataframe può essere costruito da uno di questi: Dict di 1D narray, elenchi, dadi o serie; numpy.ndarray 2-D; Strutturato o record ndarray; A Series; Un altro DataFrame. Il più semplice è un dettato di list/dict, ma non è chiaro come i tuoi dati possano essere forzati in questo modo. Cos'hai in mente? – hughdbrown

risposta

12

Ecco una soluzione che utilizza py-wikimarkup e PyQuery per estrarre tutte le tabelle come DataFrame panda da una stringa wikimarkup, ignorando il contenuto non di tabella.

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 

Dato il seguente ingresso,

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 restituisce i seguenti 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 
1

Modificato - completa la risposta di seguito. Non ho installato Panda, quindi fammi sapere se questo funziona per te.

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

Ok, ho appena guardato i documenti Panda (avrei dovuto farlo prima), e vedo esattamente ciò di cui hai bisogno ora. Dammi cinque minuti e avrò un esempio perfetto. – pycoder112358

2

Usa re a fare un po 'di pre-elaborazione, e quindi utilizzare read_csv per convertirlo in un DataFrame:

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) 

uscita:

 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" 
Problemi correlati