Getting Started With Graph Analysis in Python With Pandas and Networkx
Getting Started With Graph Analysis in Python With Pandas and Networkx
1 of 8 4/25/2021, 3:23 PM
Getting started with graph analysis in Python with pandas and networkx |... https://towardsdatascience.com/getting-started-with-graph-analysis-in-py...
2 of 8 4/25/2021, 3:23 PM
Getting started with graph analysis in Python with pandas and networkx |... https://towardsdatascience.com/getting-started-with-graph-analysis-in-py...
import pandas as pd
df = pd.DataFrame({'ID':[1,2,3,4,5,6],
'First Name':['Felix', 'Jean', 'James', 'Daphne', 'James', 'Peter'],
'Family Name': ['Revert', 'Durand', 'Wright', 'Hull', 'Conrad', 'Donovan'],
'Phone number': ['+33 6 12 34 56 78', '+33 7 00 00 00 00', '+33 6 12 34 56 78'
'Email': ['felix.revert@gmail.com', 'jean.durand@gmail.com', 'j.custom@gmail.com'
3 of 8 4/25/2021, 3:23 PM
Getting started with graph analysis in Python with pandas and networkx |... https://towardsdatascience.com/getting-started-with-graph-analysis-in-py...
4 of 8 4/25/2021, 3:23 PM
Getting started with graph analysis in Python with pandas and networkx |... https://towardsdatascience.com/getting-started-with-graph-analysis-in-py...
# By joining the data with itself, people will have a connection with themselves.
# Remove self connections, to keep only connected people who are different.
d = data_to_merge[~(data_to_merge[column_ID]==data_to_merge[column_ID+"_2"])] \
.dropna()[[column_ID, column_ID+"_2", column_edge]]
# To avoid counting twice the connections (person 1 connected to person 2 and person 2 connected to person 1
# we force the first ID to be "lower" then ID_2
d.drop(d.loc[d[column_ID+"_2"]<d[column_ID]].index.tolist(), inplace=True)
import networkx as nx
G.add_nodes_from(nodes_for_adding=df.ID.tolist())
5 of 8 4/25/2021, 3:23 PM
Getting started with graph analysis in Python with pandas and networkx |... https://towardsdatascience.com/getting-started-with-graph-analysis-in-py...
6 of 8 4/25/2021, 3:23 PM
Getting started with graph analysis in Python with pandas and networkx |... https://towardsdatascience.com/getting-started-with-graph-analysis-in-py...
7 of 8 4/25/2021, 3:23 PM
Getting started with graph analysis in Python with pandas and networkx |... https://towardsdatascience.com/getting-started-with-graph-analysis-in-py...
8 of 8 4/25/2021, 3:23 PM