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?
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.