การแทนข้อมูลเครือข่ายด้วย 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 2 ชุด: รายการโหนดและรายการเอดจ์
การวิเคราะห์เครือข่ายระดับกลางใน Python

รายการโหนดและรายการเอดจ์

  • รายการโหนด
    • แต่ละแถวคือโหนดหนึ่งโหนด
    • คอลัมน์แทนข้อมูล metadata ของโหนดนั้น
  • รายการเอดจ์
    • แต่ละแถวคือเอดจ์หนึ่งเอดจ์
    • คอลัมน์แทนข้อมูล metadata ของเอดจ์นั้น
การวิเคราะห์เครือข่ายระดับกลางใน 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...