G. Deg
G. Deg

Reputation: 628

how to read from pandas dataframe with networkx versions before 2.0

I need to generate a networkx graph with arc attributes from a pandas dataframe. In networkx version 2.0, I am aware of the from_pandas_dataframe function and I did what I wanted as follows:

graph=nx.from_pandas_dataframe(df_t, 'node2', 'node1', ['Transit Time','arctype','node1type','node2type','cpt'],nx.DiGraph())

However at the moment I have to use networkx 1.9., which does not have from_pandas_dataframe function. I was wondering how I can do that. Any help will be appreciated.

P.S. I am deploying my application in a framework which has only networkx v1.9. I don't have time to upgrade the networkx version in the framework so I am trying to change my own code.

Upvotes: 1

Views: 620

Answers (1)

unutbu
unutbu

Reputation: 879571

Using IPython, you can find the file which defines nx.from_pandas_dataframe:

In [125]: import networkx as nx
In [126]: nx.from_pandas_dataframe?
...
File:      ~/.virtualenvs/muffy/lib/python3.4/site-packages/networkx/convert_matrix.py

Inspecting this file (or the source on github) shows from_pandas_dataframe is fairly simple and depends only on one helper function, _prep_create_using.

So you could create a file utils_networkx.py and insert there the definitions of _prep_create_using and from_pandas_dataframe. Just be sure to place utils_networkx.py in a directory listed in your PYTHONPATH environment variable so that Python can import it as a module.

Then you could use it like this:

import numpy as np
import pandas as pd
import networkx as nx
import utils_networkx as UNX

r = np.random.RandomState(seed=5)
ints = r.randint(1, 10, size=(3,2))
a = ['A', 'B', 'C']
b = ['D', 'A', 'E']
df = pd.DataFrame(ints, columns=['weight', 'cost'])
df[0] = a
df['b'] = b
print(df)
#    weight  cost  0  b
# 0       4     7  A  D
# 1       7     1  B  A
# 2      10     9  C  E
G = UNX.from_pandas_dataframe(df, 0, 'b', ['weight', 'cost'])
print(G['E']['C']['weight'])
# 10
print(G['E']['C']['cost'])
# 9

The code above comes from the from_pandas_dataframe docstring.

Upvotes: 2

Related Questions