1

I am trying to create a bipartite graph from an excel file that looks similar to this:

     xyz pqr tsu
abc  -1   1  -2 
def  -2  -1   2 
ghj   2  -1   1

For begining, I have tried the following:

import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd
import xlrd

import numpy as np
from numpy import genfromtxt

df = pd.read_csv (r'C:\Users\Dragos\Desktop\networkx project\proiect.csv')
G=nx.read_edgelist('proiect.csv', create_using=nx.Graph(), nodetype=str)
nx.draw(G)
plt.show()

But I keep getting the error Failed to convert edge data (['wage,carbon', 'tax,imigration,healthcare,voting,drugs,dc', 'statehood,abortion,UBI,wealthtax']) to dictionary.

Right now I'm at a loss and not sure how to proceed.

Ene Dragos
  • 11
  • 1
  • Hi! Can you share the csv file you are reading? – miquelvir Mar 26 '21 at 13:30
  • Hello! This is the file:https://easyupload.io/38bsr9 – Ene Dragos Mar 26 '21 at 13:32
  • Try adding the correct separator ',' to read_edgelist (https://networkx.org/documentation/networkx-1.10/reference/generated/networkx.readwrite.edgelist.read_edgelist.html). – miquelvir Mar 26 '21 at 14:29
  • Even after adding ',' to separate value, I still get the same error – Ene Dragos Mar 26 '21 at 16:23
  • Don't be cnfused, the error has changed. Now instead of 'wage,carbon' you have 'min wage' and 'carbon tax', that is, it is parsing the columns properly. – miquelvir Mar 26 '21 at 16:30
  • If you need us to help, please provide a more concrete explanation on what does -1 -2 1 2 mean. – miquelvir Mar 26 '21 at 16:33
  • @miquelvir and how could I proceed after the new error? In my csv file I have the relationship between a politicians and policies: ranging from most progressive (-2), to progressive (-1), conservative(1) and most conservative(2). What I want to do is to show graphically this relationship between the two classes of nodes (politicians and policies). – Ene Dragos Mar 26 '21 at 20:14
  • added an answer – miquelvir Mar 26 '21 at 20:37

1 Answers1

0

First thing is loading the pandas dataframe

pd.read_csv(path, sep=',') 

See more here.

Then you need to create a new dataframe such that it follows this format.

>>> df
   weight  cost  0  b
0       4     7  A  D
1       7     1  B  A
2      10     9  C  E

G=nx.from_pandas_dataframe(df, 0, 'b', ['weight', 'cost'])

Check this as well.

miquelvir
  • 1,748
  • 1
  • 7
  • 21