2016-02-09 48 views
1

所以,我嘗試使用代碼形式本書:數據之實踐科學食譜代碼蟒蛇

www.ebooks-it.net/ebook/practical-data-science-cookbook

正是這種之一:

enter image description here

enter image description here

我是新來的Python所以林不知道說我期運用是正確的。我試圖這樣做:

import numpy 
import pandas 
import networkx as nx 
import unicodecsv as csv 

path = pandas.read_csv("hero-network.csv", low_memory=False) 

def graph_from_csv(path): 
    graph = nx.Graph(name="Heroic Social Network") 
    with open(path, 'rU') as data: 
     reader = csv.reader(data) 
     for row in reader: 
      graph.add_edge(*row) 
    return graph.size() 

graph_from_csv(data) 

但我沒有得到像書中一樣的輸出。

我的輸出是:

TypeError: invalid file:    LITTLE, ABNER  PRINCESS ZANDA 
0    LITTLE, ABNER BLACK PANTHER/T'CHAL 
1  BLACK PANTHER/T'CHAL  PRINCESS ZANDA 
2    LITTLE, ABNER  PRINCESS ZANDA 
3    LITTLE, ABNER BLACK PANTHER/T'CHAL 
4  BLACK PANTHER/T'CHAL  PRINCESS ZANDA 
5  STEELE, SIMON/WOLFGA  FORTUNE, DOMINIC 
6  STEELE, SIMON/WOLFGA ERWIN, CLYTEMNESTRA 
7  STEELE, SIMON/WOLFGA IRON MAN/TONY STARK 
8  STEELE, SIMON/WOLFGA IRON MAN IV/JAMES R. 
9  STEELE, SIMON/WOLFGA RAVEN, SABBATH II/EL 
10  RAVEN, SABBATH II/EL  FORTUNE, DOMINIC 
11  RAVEN, SABBATH II/EL ERWIN, CLYTEMNESTRA 
12  RAVEN, SABBATH II/EL IRON MAN/TONY STARK 
13  RAVEN, SABBATH II/EL IRON MAN IV/JAMES R. 
14  IRON MAN IV/JAMES R.  FORTUNE, DOMINIC 
15  IRON MAN IV/JAMES R. ERWIN, CLYTEMNESTRA 
16  IRON MAN IV/JAMES R. IRON MAN/TONY STARK 
17  IRON MAN/TONY STARK  FORTUNE, DOMINIC 
18  IRON MAN/TONY STARK ERWIN, CLYTEMNESTRA 
19  ERWIN, CLYTEMNESTRA  FORTUNE, DOMINIC 
20   PRINCESS ZANDA BLACK PANTHER/T'CHAL 
21   PRINCESS ZANDA   LITTLE, ABNER 
22    LITTLE, ABNER BLACK PANTHER/T'CHAL 
23  BLACK PANTHER/T'CHAL   LITTLE, ABNER 
24  BLACK PANTHER/T'CHAL  PRINCESS ZANDA 
25  BLACK PANTHER/T'CHAL CARNIVORE/COUNT ANDR 
26  BLACK PANTHER/T'CHAL   LITTLE, ABNER 
27    LITTLE, ABNER  PRINCESS ZANDA 
28    LITTLE, ABNER CARNIVORE/COUNT ANDR 
29  CARNIVORE/COUNT ANDR  PRINCESS ZANDA 
        ...     ... 
574436 CANNONBALL II/SAM GU WASP/JANET VAN DYNE 
574437 WASP/JANET VAN DYNE    ROGUE/
574438 WASP/JANET VAN DYNE    CALIBAN/ 
574439 WASP/JANET VAN DYNE    CALLISTO 
574440 WASP/JANET VAN DYNE COLOSSUS II/PETER RA 
574441 WASP/JANET VAN DYNE     SELENE 
574442 WASP/JANET VAN DYNE PROFESSOR X/CHARLES 
574443 WASP/JANET VAN DYNE PHOENIX III/RACHEL S 
574444 PHOENIX III/RACHEL S    ROGUE/
574445 PHOENIX III/RACHEL S    CALIBAN/ 
574446 PHOENIX III/RACHEL S    CALLISTO 
574447 PHOENIX III/RACHEL S COLOSSUS II/PETER RA 
574448 PHOENIX III/RACHEL S    SELENE 
574449 PHOENIX III/RACHEL S PROFESSOR X/CHARLES 
574450 PROFESSOR X/CHARLES    ROGUE/
574451 PROFESSOR X/CHARLES    CALIBAN/ 
574452 PROFESSOR X/CHARLES    CALLISTO 
574453 PROFESSOR X/CHARLES COLOSSUS II/PETER RA 
574454 PROFESSOR X/CHARLES     SELENE 
574455    SELENE    ROGUE/
574456    SELENE    CALIBAN/ 
574457    SELENE    CALLISTO 
574458    SELENE COLOSSUS II/PETER RA 
574459 COLOSSUS II/PETER RA    ROGUE/
574460 COLOSSUS II/PETER RA    CALIBAN/ 
574461 COLOSSUS II/PETER RA    CALLISTO 
574462    CALLISTO    ROGUE/
574463    CALLISTO    CALIBAN/ 
574464    CALIBAN/    ROGUE/
574465 HULK/DR. ROBERT BRUC  MARKS, DR. SHIELA 

[574466 rows x 2 columns] 
+0

*你得到了什麼輸出? – Andy

+0

數據集是否與本書中的示例完全相同? – cdarke

+0

我編輯添加我的輸出 – user3088738

回答

2

我想有一個你的示例代碼有一點差錯。最後一行應該是graph_from_csv("hero_network.csv")(而不是data這是一個DataFrame)?

這解釋了TypeError輸出(因爲您稍後嘗試使用使用此DataFrame作爲參數的open(),函數期望將該文件的路徑作爲字符串)。

def graph_from_csv(path): 
    graph = nx.Graph(name="Heroic Social Network") 
    with open(path, 'rU') as data: 
     reader = csv.reader(data) 
     for row in reader: 
      graph.add_edge(*row) 
    return graph.size() 

graph_from_csv("hero-network.csv") 

Out[15]: 167219