Building Network Graphs from Pandas’ Dataframe

Nwk Graphs From DataFrame FeaImg

In this tutorial, we will attempt to generate an amazing and interactive network graph from a pandas data frame to take things up a notch!

Also Read: NetworkX Package – Python Graph Library

Without any delay, Let’s begin!

Loading and Pre-processing Data

This section is focused on loading and pre-processing the dataset. The dataset chosen for this tutorial is the OpenFlights Airport dataset available on Kaggle. As of January 2017, the OpenFlights Airports Database contains data for over 10,000 airports over the globe.

Read More: Working with DataFrame Rows and Columns in Python

In the code below, we will be importing the pandas module and load the routes.csv file into the program. Now out of all the columns in the dataset, we only require the source and destination airports from the dataset.

import pandas as pd
df = pd.read_csv('routes.csv')
df = df[['Source airport','Destination airport']]
df = df[:500]

To make the processing easier and the computation less complex, we will only take the top 500 rows from the dataset. And we will display the first five rows of the dataset using the head function.

OpenFlight Dataset 5 Rows
OpenFlight Dataset 5 Rows

We will separate the sources and destination nodes into two separate lists using the Python code below.

sources = list(df['Source airport'])
destinations = list(df['Destination airport'])

Now we will move on to the generation of the Network graph using the networkx and pyviz library in the next section.

Generation of Network Graph

We will start off by creating an empty graph using the net.Network function and passing a number of attributes of the empty network graph. The next step is to iterate over the sources list and add nodes along with their labels and titles.

After this, we will be adding edges using the add_edge function. We will be making use of exception handling to make sure all errors are taken into consideration (if any).

Also Read: Python Exception Handling – Try, Except, Finally

Look at the code mentioned below.

g_from_data =net.Network(height='600px',width='50%',
              heading="A Networkx Graph from DataFrame",directed=True)

for i in range(len(sources)):

for (i,j) in zip(sources,destinations):


Have a look at the network graph generated below. It’s amazing how the graph looks this interesting and fun to check out.


I hope you were able to understand how to generate network graphs using the pandas data frame using the pyviz library in Python programming language. Thank you for reading!

I would recommend you to have a read at the following tutorials below:

  1. Network Analysis in Python – A Complete Guide
  2. Neural Networks in Python – A Complete Reference for Beginners
  3. An Ultimate Guide On Insider Threat Risks And Their Prevention