2013-03-30 41 views
9

Trong Wikipedia, bạn có thể tìm thấy một số dữ liệu thú vị để được sắp xếp, lọc, ...Làm thế nào để chuyển đổi Wikipedia wikitable sang Python Pandas DataFrame?

Dưới đây là một mẫu của một 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" 
|- 
|} 

Tôi đang tìm kiếm một cách để nhập dữ liệu như vậy để a Python Pandas DataFrame

+0

Theo này: http://pandas.pydata.org/pandas-docs/dev/dsintro.html#dataframe một DataFrame thể được xây dựng từ một trong những: Dict của 1D ndarrays, danh sách, dicts, hoặc Series; 2-D numpy.ndarray; Kết cấu có cấu trúc hoặc ghi lại; A Series; Một DataFrame khác. Đơn giản nhất là một dict của danh sách/dict, nhưng nó không phải là rõ ràng như thế nào dữ liệu của bạn có thể được ép buộc theo cách này. Bạn đang nghĩ gì vậy? – hughdbrown

Trả lời

12

Dưới đây là một giải pháp sử dụng py-wikimarkupPyQuery để trích xuất tất cả các bảng dưới dạng các khung dữ liệu gấu trúc từ một chuỗi wikimarkup, bỏ qua nội dung không phải bảng.

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 

Với đầu vào sau,

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 trả về DataFrames sau.

 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

Đã chỉnh sửa - câu trả lời đầy đủ bên dưới. Tôi không có Panda cài đặt, vì vậy hãy cho tôi biết nếu điều này làm việc cho bạn.

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, tôi chỉ nhìn vào tài liệu Panda (nên đã làm điều đó đầu tiên), và tôi thấy chính xác những gì bạn cần bây giờ. Hãy cho tôi năm phút và tôi sẽ có một ví dụ hoàn hảo. – pycoder112358

2

Sử dụng re để làm một số preprocess, và sau đó sử dụng read_csv để chuyển nó sang một 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) 

đầu ra:

 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"