简体   繁体   中英

Source and target IDs must be 0-based integers, found types [dtype('O'), dtype('O')] error when generating a network graph using iGraph

I have a df that ranks the Importance of TF and Target .networks in descending order. I now want to generate a graph using the iGraph Python library.

My code raised TypeError: Source and target IDs must be 0-based integers, found types [dtype('O'), dtype('O')] .

from igraph import Graph
import pandas as pd

Identify the edges and vertices:

edges = df.iloc[:,0:2]
vertices = pd.DataFrame(df["Importance"])

Graph generation:

g = Graph.DataFrame(edges, directed=False, vertices=vertices)

Traceback:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_16/1842214905.py in <module>
----> 1 g = Graph.DataFrame(edges, directed=False, vertices=vertices)

/opt/conda/lib/python3.7/site-packages/igraph/io/objects.py in _construct_graph_from_dataframe(cls, edges, directed, vertices, use_vids)
    423         ):
    424             raise TypeError(
--> 425                 f"Source and target IDs must be 0-based integers, found types {edges.dtypes.tolist()[:2]}"
    426             )
    427         elif (edges.iloc[:, :2] < 0).any(axis=None):

TypeError: Source and target IDs must be 0-based integers, found types [dtype('O'), dtype('O')]

df.head()

pd.DataFrame({'TF': {0: 'ZFY', 1: 'ZFY', 2: 'ZFY', 3: 'ZFY', 4: 'ZFY'},
 'Target': {0: 'DDX3Y', 1: 'EIF1AY', 2: 'CYorf15A', 3: 'USP9Y', 4: 'KDM5D'},
 'Importance': {0: 271.64476419966564,
  1: 249.63252368981105,
  2: 249.47948849863877,
  3: 242.14502589211688,
  4: 215.67076799218304}})

By default Graph.DataFrame interprets the first two columns as integer vertex IDs. By specifying use_vids=False they will be interpreted as vertex "names" instead of integer vertex IDs. In your case, you would want to specify use_vids=False . See the documentation for some additional explanation.

Note that you do not need to specify the vertices argument. It seems that you are now including the Importance score as a vertex argument, but from your example, it seems that this is actually an edge attribute (a sort of edge weight). You should hence be able to simply do

G = ig.Graph.DataFrame(df, directed=False, use_vids=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM