1

I'm currently trying to generate a Network-Graph with NetworkX on Python. The Dataset has the following format (it's a CSV):

Id, Tag
0, 'science'
0, 'history'
1, 'sports'
3, 'sci-fi'
3, 'history'
3, 'music'

The Goal is to show the relation between the tags. F.e. Science is really closely related to History because they share the same Id. Now here's where I'm stuck: How can I create a Table/Matrix that has 2 columns for NetworkX to use as Source(-Tag) and Target(-Tag) with Python?

I tried creating a Dataframe with Pandas and then using the same column twice, but that didn't work for me.

6
  • How is sports related to history? Commented Apr 2, 2020 at 13:57
  • It's related because they have the same id Commented Apr 2, 2020 at 14:01
  • But they don't. Sports' id is 1. Commented Apr 2, 2020 at 14:02
  • My bad, I corrected it. Commented Apr 2, 2020 at 14:03
  • What did you try so far? Please provide a minimal an verifiable example Commented Apr 2, 2020 at 14:04

1 Answer 1

1

To connect nodes based on the Id, here's one approach, you can gruopby the Id, aggregate the groups to lists, and build a directed graph adding the lists as paths:

df = pd.read_csv('my_file.csv', sep=', ')
l = df.groupby('Id').Tag.agg(list).tolist()
# [['science', 'history'], ['sports'], ['sci-fi', 'history', 'music']]

G=nx.Graph()
for sl in l:
    nx.add_path(G, sl)

nx.draw(G, node_color='lightblue', 
        with_labels=True, 
        node_size=800)

enter image description here


For a directed graph:

G=nx.DiGraph()
for sl in l:
    nx.add_path(G, sl)

nx.draw(G, node_color='lightblue', 
        with_labels=True, 
        node_size=800)

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, that's exactly what i was looking for! :) Do you know if there's any way to optimize l = df.groupby('Id').Tag.agg(list).tolist() ? It get's really slow with bigger sets...
No prob :) Try df.groupby('Id').Tag.agg(list).values.tolist() @HelloNirvanaNorway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.