{ "cells": [ { "cell_type": "markdown", "id": "weird-kinase", "metadata": {}, "source": [ "# Graphing Network Data with Pandas" ] }, { "cell_type": "markdown", "id": "actual-essex", "metadata": {}, "source": [ "## Getting the Data from Pandas to NetworkX" ] }, { "cell_type": "markdown", "id": "grand-casino", "metadata": {}, "source": [ "Pandas on its own cannot plot out network data. Instead, we must rely on two other libraries, NetworkX and Matplotlib. NetworkX is the standard Python library for working with networks. I have a forthcoming textbook, like this one, that walks users through NetworkX. Matplotlib is one of the standard plotting libraries. The purpose of this brief section, is to provide the code necessary for making Pandas work with NetworkX and Matplotlib to take networks stored in a Pandas DataFrame and transform the relationships into graphs. We will address social networks in greater detail in Part 4 of this textbook." ] }, { "cell_type": "code", "execution_count": 1, "id": "lesbian-jungle", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import networkx as nx\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "id": "hidden-silicon", "metadata": {}, "source": [ "Let's now load our data and see what it looks like." ] }, { "cell_type": "code", "execution_count": 2, "id": "amino-consciousness", "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv(\"data/network.csv\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "express-evans", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | source | \n", "target | \n", "
---|---|---|
0 | \n", "Tom | \n", "Rose | \n", "
1 | \n", "Rose | \n", "Rosita | \n", "
2 | \n", "Jerry | \n", "Jeff | \n", "
3 | \n", "Jeff | \n", "Larry | \n", "
4 | \n", "Carmen | \n", "Carmen | \n", "
5 | \n", "Rosita | \n", "Rosita | \n", "
6 | \n", "Larry | \n", "Carmen | \n", "
7 | \n", "Larry | \n", "Jerry | \n", "