用 pandas 表示網路資料

Python 網路分析進階

Eric Ma

Data Carpentry instructor and author of nxviz package

以 CSV 儲存網路資料

  • CSV 檔
person,party,weight
Barrett.Samuel,LondonEnemies,1
Barrett.Samuel,StAndrewsLodge,1
Marshall.Thomas,LondonEnemies,1
Eaton.Joseph,TeaParty,1
Bass.Henry,LondonEnemies,1
Python 網路分析進階

以 CSV 儲存網路資料

  • 優點:
    • 易讀(人類可讀)
    • 可用 pandas 進一步分析
  • 缺點:
    • 重複、占用磁碟空間
  • 兩個 DataFrame:節點清單與邊清單
Python 網路分析進階

節點清單與邊清單

  • 節點清單
    • 每列代表一個節點
    • 欄位是該節點的中繼資料
  • 邊清單
    • 每列代表一條邊
    • 欄位是該邊的中繼資料
Python 網路分析進階

Pandas 與圖形

list(G.nodes(data=True))
[(0, {'bipartite': 0}),
(1, {'bipartite': 0}),
(2, {'bipartite': 0}),
...]
nodelist = []

for n, d in G.nodes(data=True): node_data = dict() node_data['node'] = n
node_data.update(d)
nodelist.append(node_data)
Python 網路分析進階

Pandas 與圖形

nodelist
[{'bipartite': 0, 'node': 0},
{'bipartite': 0, 'node': 1},
{'bipartite': 0, 'node': 2},
{'bipartite': 0, 'node': 3},
{'bipartite': 0, 'node': 4},...]
Python 網路分析進階

Pandas 與圖形

import pandas as pd
pd.DataFrame(nodelist)
  bipartite  node
0           0     0
1           0     1
2           0     2
3           0     3
4           0     4
5           1     5
6           1     6
7           1     7
pd.DataFrame(nodelist).to_csv('my_file.csv')
Python 網路分析進階

一起來練習吧!

Python 網路分析進階

Preparing Video For Download...