German Portes
German Portes

Reputation: 5

Create python network graph from 3 dataframe columns unweighted using networkx and pyvis

Im working to create a python network graph utilizing the 3 pandas dataframe string columns - unweighted (python networks and pyviz)

I would like to link (nodes) the projects to country, and projects to company.

my table/dataframe is as follow;

Company Project Country
T-mobile 5G upgrade Germany
T-mobile Network upgrade Kuwait

Upvotes: 0

Views: 712

Answers (1)

Riccardo Bucco
Riccardo Bucco

Reputation: 15364

Let's say the DataFrame containing your data is df (and it has three columns, i.e. "Company", "Project" and "Country". This would be a possible solution:

import networkx as nx
import pandas as pd

G = nx.from_pandas_edgelist(
    pd.DataFrame({
        'source': pd.concat((df['Project'], df['Project'])),
        'target': pd.concat((df['Company'], df['Country']))
    }),
)

Here are the nodes and edges of G:

>>> G.nodes
NodeView(('5G upgrade', 'T-mobile', 'Network upgrade', 'Germany', 'Kuwait'))
>>> G.edges
EdgeView([('5G upgrade', 'T-mobile'), ('5G upgrade', 'Germany'),
('T-mobile', 'Network upgrade'), ('Network upgrade', 'Kuwait')])

Upvotes: 0

Related Questions