I am working with a python package called "cdindex" that gets data in network/graph structure (nodes and edges) and after creating a graph, it generates some integer properties of the nodes.
Following the package example, I have created the nodes (here vertices) and edges data as follows:
>>> $ pip install cdindex
>>> import cdindex
>>> import datetime
>>> # dummy vertices for python module tests
>>> pyvertices= [{"name": "0Z", "time": datetime.datetime(1992, 1, 1)},
{"name": "1Z", "time": datetime.datetime(1992, 1, 1)},
{"name": "2Z", "time": datetime.datetime(1993, 1, 1)},
{"name": "3Z", "time": datetime.datetime(1993, 1, 1)},
{"name": "4Z", "time": datetime.datetime(1995, 1, 1)},
{"name": "5Z", "time": datetime.datetime(1997, 1, 1)},
{"name": "6Z", "time": datetime.datetime(1998, 1, 1)},
{"name": "7Z", "time": datetime.datetime(1999, 1, 1)},
{"name": "8Z", "time": datetime.datetime(1999, 1, 1)},
{"name": "9Z", "time": datetime.datetime(1998, 1, 1)},
{"name": "10Z", "time": datetime.datetime(1997, 1, 1)}]
>>> # dummy edges for python module tests
>>> pyedges = [{"source": "4Z", "target": "2Z"},
{"source": "4Z", "target": "0Z"},
{"source": "4Z", "target": "1Z"},
{"source": "4Z", "target": "3Z"},
{"source": "5Z", "target": "2Z"},
{"source": "6Z", "target": "2Z"},
{"source": "6Z", "target": "4Z"},
{"source": "7Z", "target": "4Z"},
{"source": "8Z", "target": "4Z"},
{"source": "9Z", "target": "4Z"},
{"source": "9Z", "target": "1Z"},
{"source": "9Z", "target": "3Z"},
{"source": "10Z", "target": "4Z"}]
Next, I created the graph and added the nodes (here vertices) and edges to it:
>>> # create graph
>>> graph = cdindex.Graph()
>>> # add vertices
>>> for vertex in pyvertices:
graph.add_vertex(vertex["name"], cdindex.timestamp_from_datetime(vertex["time"]))
>>> # add edges
>>> for edge in pyedges:
graph.add_edge(edge["source"], edge["target"])
The code runs nicely, and I am able to initiate the "graph.cdindex" function to get a node's (in example case 4Z) cdindex as an integer value in output (i.e. 0.833).
>>> graph.cdindex("4Z", int(datetime.timedelta(days=1825).total_seconds()))
[Out] 0.8333333333333333
My question
I want to learn how I can loop multiple values to the "graph.cdindex" function and save the results in data frame. Technically, instead of "4Z" string value in the above function, I want to loop a column of many other string values (0Z, 1Z, 2Z, ...) and save their associated cdindex value in a new column.
Deliverable
A dataframe of all nodes (Node_Name) and their cdindex value (cdindex_Value) like below:
----------- ---------------
| Node_Name | cdindex_Value |
----------- ---------------
| 0Z | 0.247 |
----------- ---------------
| 1Z | 0.654 |
----------- ---------------
| 2Z | -0.547 |
----------- ---------------
| 3Z | 0.0 |
----------- ---------------
| 4Z | 0.8333 |
----------- ---------------
CodePudding user response:
You can use a list comprehension to get all of the vertices and associated cindex values:
tuples = [(v, graph.cdindex(v, int(datetime.timedelta(days=1825).total_seconds()))) for v in graph.vertices()] # check how to get the vertices name
df = DataFrame(tuples, columns=['node', 'cindexvalue'])