Convert Graph object to dataframe

Hi all,

I have a question regarding a function which exists on R but I can not find it on Python. I have my graph object, and I want to transform it into a dataframe composed of vertices and vertice’s attributes. The equivalent function on R is ‘as_data_frame()’ but it does not exist on Python.

So I want to have the ‘Graph.DataFrame()’ function but in the opposite way.

I hope you understand my worries.

Thanks for answering

You can easily do that manually:

node_df = pd.DataFrame({attr: G.vs[attr] for attr in G.vertex_attributes()})
edge_df = pd.DataFrame({attr:[attr] for attr in G.edge_attributes()})

Given that we now have a Graph.DataFrame constructor, it may make sense to add the above two things as Graph.get_node_dataframe and Graph.get_edge_dataframe. What do you think @tamas, @iosonofabio?

Sure, happy to do it

1 Like

When properly implementing this, it may make sense to that the vertex names as indices for the dataframe, and perhaps also include the source and target nodes in the edge dataframe?

Both reasonable I would say


It works! Perfect!

Thank you!

1 Like

I just tried the function to have a df composed of edges. it does not work properly.
In fact, I have the accurate number of edges but the output contains 1 column composed of my attribute ‘weight’. Do you know how could I also have to name of my nodes (from and to)? To make a df composed of 3 columns : name1 || name2 ||weight.

Thank you again.

Your help and reactivity are so grateful!