Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ™ Feature request: funtion to import nodes.csv & edges.csv (aka. " Gephi Import CSV Data") #56

Open
2 tasks
adriens opened this issue Sep 21, 2024 · 2 comments

Comments

@adriens
Copy link
Contributor

adriens commented Sep 21, 2024

❔ About

In various graph dataviz tools (like Gephi) it is very common to import files to create a graph with csv:

  • nodes.csv
  • edges.csv

The main idea would to make it easy to do the same on yFiles.

πŸ’‘ Feature request

⏳ Workaround

As a workaround I'm using the graphml import feature

@adriens adriens changed the title πŸ™ Feature request: funtion to import nodes.csv & edges.csv πŸ™ Feature request: funtion to import nodes.csv & edges.csv (aka. _"Import CSV Data"_ ) Sep 21, 2024
@adriens adriens changed the title πŸ™ Feature request: funtion to import nodes.csv & edges.csv (aka. _"Import CSV Data"_ ) πŸ™ Feature request: funtion to import nodes.csv & edges.csv (aka. "Import CSV Data") Sep 21, 2024
@adriens adriens changed the title πŸ™ Feature request: funtion to import nodes.csv & edges.csv (aka. "Import CSV Data") πŸ™ Feature request: funtion to import nodes.csv & edges.csv (aka. " Gephi Import CSV Data") Sep 21, 2024
@yGuy
Copy link
Member

yGuy commented Sep 23, 2024

That might work:

import pandas as pd

# Read CSV files
nodes_df = pd.read_csv('nodes.csv')
edges_df = pd.read_csv('edges.csv')

# Create a new graph widget
w = GraphWidget()

# Add nodes with all attributes
w.nodes = []
for _, row in nodes_df.iterrows():
    node_attrs = row.to_dict()
    node_id = node_attrs.pop('id')  # Assuming 'id' is the column for node identifiers
    w.nodes.append({
        "id": node_id,
        "properties": node_attrs
    })

# Add edges with all attributes
w.edges = []
for _, row in edges_df.iterrows():
    edge_attrs = row.to_dict()
    edge_id = edge_attrs.pop('id', None)  # Assuming 'id' is the column for edge identifiers
    source = edge_attrs.pop('source')  # Assuming 'source' is the column for edge source
    target = edge_attrs.pop('target')  # Assuming 'target' is the column for edge target
    w.edges.append({
        "id": edge_id if edge_id is not None else f"{source}-{target}",
        "start": source,
        "end": target,
        "properties": edge_attrs
    })

# Set the graph as directed (you can change this if your graph is undirected)
w.directed = True

# Display the graph
display(w)

@adriens
Copy link
Contributor Author

adriens commented Oct 5, 2024

Thanks a lot for the code. I'll give it a try very soon.
I'll release a content on yWorks/yfiles-jupyter-graphs next week, with the graphml import, including a blog post and a video

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants