Krishna Neupane
Krishna Neupane

Reputation: 443

AttributeError: module 'networkx' has no attribute 'from_pandas_dataframe'

I have networkx v. 2.1. to make it work w/ pandas dataframe, i tried following:

Error description.

AttributeError: module 'networkx' has no attribute 'from_pandas_dataframe`

Steps to reproduce Error:

I imported data using csv. I did this because I just wanted to read only 5000 rows from the dataset.

x=pd.DataFrame([x for x in rawData[:5000]])

x[:10] 

0   1   2
0   228055  231908  1
1   228056  228899  1
2   228050  230029  1
3   228059  230564  1
4   228059  230548  1
5   70175   70227   1
6   89370   236886  1
7   89371   247658  1
8   89371   249558  1
9   89371   175997  1

g_data=G=nx.from_pandas_dataframe(x)

module 'networkx' has no attribute 'from_pandas_dataframe'

I know I am missing the from_pandas_dataframe but cant find a way to install it.

[m for m in nx.__dir__() if 'pandas' in m] 

['from_pandas_adjacency',
 'to_pandas_adjacency',
 'from_pandas_edgelist',
 'to_pandas_edgelist']

Upvotes: 45

Views: 45954

Answers (3)

tohv
tohv

Reputation: 970

In networkx 2.0 from_pandas_dataframe has been removed.

Instead you can use from_pandas_edgelist.

Then you'll have:

g_data=G=nx.from_pandas_edgelist(x, 1, 2, edge_attr=True)

Upvotes: 83

William Pourmajidi
William Pourmajidi

Reputation: 1036

A simple graph:

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

# Build a dataframe with 4 connections
df = pd.DataFrame({'from': \['A', 'B', 'C', 'A'\], 'to': \['D', 'A', 'E', 'C'\]})

# Build your graph
G = nx.from_pandas_edgelist(df, 'from', 'to')

# Plot it
nx.draw(G, with_labels=True)
plt.show()

enter image description here

Upvotes: 14

Krishna Neupane
Krishna Neupane

Reputation: 443

This solution helps as well:

G = nx.DiGraph()
G.add_weighted_edges_from([tuple(x) for x in x.values])
nx.info(G)


'Name: \nType: DiGraph\nNumber of nodes: 5713\nNumber of edges: 5000\nAverage in degree:   0.8752\nAverage out degree:   0.8752'

Upvotes: -5

Related Questions