user2723240
user2723240

Reputation: 823

NetworkX From_Pandas_dataframe

I have an error with NetworkX which says 'module' has no attribute 'from_pandas_dataframe.'

I have a dataframe called nflroster that is format as:

Index   . . . Player           Team       Year

0       . . . Player1          Team1      2014

1       .  . .Player2          Team1      2014


2       . . . Player3          Team2      2014
.
.       . . .   .                .         .

So according to the documentation here networkx documentation this following line should work

G = nx.from_pandas_dataframe(nflroster,str, 'Team')

However when I run this in Ipy notebook I run into the error, 'module' object has no attribute 'from_pandas_dataframe'.

I import the following

import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd
from pandas import DataFrame as df

Upvotes: 5

Views: 6598

Answers (4)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210842

yet another way to search for a method in Networkx API:

In [199]: [m for m in nx.__dir__() if 'pandas' in m]
Out[199]:
['from_pandas_adjacency',
 'to_pandas_adjacency',
 'from_pandas_edgelist',
 'to_pandas_edgelist']

Upvotes: 0

marianstefi20
marianstefi20

Reputation: 157

If we look in the networkx's build folder, in __init__.py, we see an import from networkx.convert_matrix. Looking thru the convert_matrix.py file we can see the following allowed external dependencies:

__all__ = ['from_numpy_matrix', 'to_numpy_matrix',
           'from_pandas_adjacency', 'to_pandas_adjacency',
           'from_pandas_edgelist', 'to_pandas_edgelist',
           'to_numpy_recarray',
           'from_scipy_sparse_matrix', 'to_scipy_sparse_matrix',
           'from_numpy_array', 'to_numpy_array']

As you can see, from_pandas_dataframe does not exist. Although this might have been in the 1.10 version.

So in the end, always keep a keen eye on the version number.

Upvotes: 2

hlin117
hlin117

Reputation: 22270

You probably installed an incorrect version of networkx. You probably should check whether you have 1.10.0 <= see history here

Upvotes: 2

Gustavo Avila
Gustavo Avila

Reputation: 86

I had the same problem here. This is how I solved it:

Try installing networkx from source instead of installing it through pip.

Source Install Step by Step

    Download the source (tar.gz or zip file) from https://pypi.python.org/pypi/networkx/ or get the latest development version from https://github.com/networkx/networkx/
    Unpack and change directory to the source directory (it should have the files README.txt and setup.py).
    Run python setup.py install to build and install

Notice that this specific function From_Pandas_dataframe will be installed in convert_matrix.py file at the networkx folder.

Upvotes: 3

Related Questions