2

I'm an absolute newbie to the Networkx package and fairly new to Python. Upon looking over a variety of Networkx examples it appears there's many ways to create plots and I'm looking for some tips.

I have a file which looks like:

Napoleon        Myriel          1
Mlle.Baptistine Myriel          8
Mme.Magloire    Myriel          10
Mme.Magloire    Mlle.Baptistine 6
CountessdeLo    Myriel          1

describing characters in a movie, with the rightmost column containing how many times they meet. The 'source' is the leftmost column and the 'target' is the middle column. I'd like to create a plot that looks similar to the one below.

Example networkx graph I'd like to emulate

I've tried so many different things I'm hesitant to provide any code here. Any basic starting tips would be very much appreciated.

1 Answer 1

2

What you're looking for is pandas. In your example, we can create a NetworkX graph from your csv file as follows:

import pandas as pd
import networkx as nx
 
df = pd.read_csv(csv_filename)
G = nx.from_pandas_edgelist(df, source='source', target='target', edge_attr='weight', create_using=nx.DiGraph())

Keep in mind that you need to assign titles to your columns in the csv file "source", "target" and "weight" for this example to work. Or you can assign different strings as titles and modify the strings in the function.

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

1 Comment

Thank you! That helped a lot and I'm now able to produce a plot with the data.

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.