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...