-1

I have an adjacency list in a csv file which is like this:

'a','b','0.5','c','0.34','e','0.25'\n

'b','c','0.78','d','0.3','e','0.25'\n

etc

The first 'column' represent the start node and after that the following 'columns' represent the nodes adjacent neighbours and their edges' weights. The file contains almost 17.000 rows and its row has 341 elements.

My question is how can I represent the former data as an undirected graph in Python?

I've already try the read_adjlist from NetworkX but this is not for weighted edges.

Rakesh
  • 81,458
  • 17
  • 76
  • 113
Georgia
  • 53
  • 5

1 Answers1

0

Until a better solution comes along you can convert your csv into a pandas dataframe and convert that dataframe into an edgelist. Then networkx has a method: from_pandas_edgelist()

import pandas as pd
import networkx as nx

df = pd.read_csv('adj.csv', header=None)
adj_df = df[[0,1,2]]
for i in range(3, df.shape[1], 2):
    df.drop(labels =[1, 2], inplace=True, axis=1)
    df.rename(columns={i:1, i+1:2}, inplace=True)
    adj_df = adj_df.append(df[[0,1,2]])


adj_df.rename(columns={2:'weight'}, inplace=True)
G=nx.from_pandas_edgelist(adj_df, 0, 1, ['weight'])
dsbailey
  • 108
  • 1
  • 1
  • 9