0

I have created node with using py2neo package

from py2neo import Graph
from py2neo import Node

This is my pandas dataframe

Pandas Dataframe

I can create successfully node.

Node Creation using pandas dataframe

I have been trying to working with relationship getting error!

graph.create(Relationship(pmid, "Having_author", auth))

TypeError: Values of type <class 'pandas.core.series.Series'> are not supported

I have also refer stack overflow question but still getting Error!

Here is the link

Is there any other way to create a relationship with pandas dataframe ?

2 Answers2

1

Your code is failing because the ID for a node must be a literal (integer or string), but you set the ID as a Series when you wrote ...id = data['PMID']) . It appears py2neo allowed you to create the node object with a faulty ID, but it really shouldn't, because all relationships with that node will fail since the ID is bad.

Recreate the Node Classes with an integer for the ID, and then the Relationship between them should be created without issues.

Note, I haven't tested this code, but this is how you would loop through a df and create nodes as you go.

for i, row in data.iterrows():
    pmid = row['PMID'] #this is the integer PMID based on your df
    pmi_node = Node("PMID row " + str(i), id=pmid) #create node

    authid = row['AU'] #this is a string author name based on your df
    auth_node = Node("Auth row " + str(i), id=authid) #create node

    graph.create(pmi_node | auth_node)

    #create a relationship between the PMI and Auth for that row
    graph.create(Relationship(pmi_node , "Having_author", auth_node))

PS -- The SO link you referenced is not using the py2neo package, but is instead simply sending cypher code strings to the database using the neo4j python package. I'd recommend this route if you are a beginner.

Cobra
  • 612
  • 6
  • 15
  • I have try with code pmid.astype(int) but still facing issue.......... – MASTANELAL ABDULGAFAR KURESHI Sep 28 '21 at 07:43
  • TypeError: Unknown node type for 0 20301691 1 33237688 2 26903338 3 24741715 4 25299040 ... 4232 26065577 4233 26065544 4234 26065469 4235 26065462 4236 26065451 Name: PMID, Length: 4237, dtype: int32 – MASTANELAL ABDULGAFAR KURESHI Sep 28 '21 at 07:48
  • It seems like you need to learn more about pandas data classes. you cannot convert a Series directly to an integer. what do you expect it to do, sum all the entries? concat? You need to make a node for each entry individually, not try to create a single node out of the entire series. I edited my answer with a sample code block. – Cobra Sep 28 '21 at 13:11
  • you have written data.iterrows() but series object can't be iterate if you know it's throwing error.......... – MASTANELAL ABDULGAFAR KURESHI Sep 28 '21 at 13:29
  • in your screenshot, data is a dataframe, not a series. if you've changed what `data` is in your code, please post an update. – Cobra Sep 28 '21 at 14:00
0

I have convert series object in zip then it works for me

for pmid, au in tqdm(zip(data.PMID, data.AU),total = data.shape[0]):
    pmid_node = Node("pmid", name=int(pmid))
    au_node = Node("author", name=au)
    graph.create(Relationship(pmid_node, "HAVING_AUTHOR", au_node))