1

Hi everyone I want to read the edge list from a csv file and create a graph with networkx to calculate the betweenness centrality with python. My code is:

import pandas as pd
import networkx as nx
df = pd.read_csv('edges1.csv')
Graphtype = nx.Graph()
G = nx.from_pandas_edgelist(df, edge_attr='genre_ids', create_using=Graphtype)
centrality = nx.betweenness_centrality(G, normalize=False)
print(centrality)

edges1.csv have 97180 row:

Surce,Target,genre_ids
Avatar,Violent Night,18
Harry Potter,The Woman King,20
Happy Feet, Froze,23
so on....

My code give me error: KeyError: 'source'. How can i do?

2
  • 2
    I didn't test it but I guess instead of Surce,Target,genre_ids in the csv you should put source,target,genre_ids, so with an o in source and lower case. Commented Jan 13, 2023 at 19:47
  • 2
    from_pandas_edgelist requires valid source column, fix your file header Commented Jan 13, 2023 at 19:49

1 Answer 1

1

When loading the data, make sure the column names in csv file match the default expected values OR specify the custom names.

If the column names are "Surce,Target,genre_ids" (as in the snippet provided by OP), then the appropriate command is:

G = nx.from_pandas_edgelist(
    df,
    source="Surce",
    target="Target",
    edge_attr='genre_ids',
    create_using=Graphtype
)

See the docs.

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

Comments

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.