Edgelist within pandas dataframe to visualise using networkx

后端 未结 1 1455
时光说笑
时光说笑 2021-01-15 22:22

I am having difficulties in representing a dataframe as a network using networkx. The problem seems to be related to the size of dataframe, or, to better explaining, to the

相关标签:
1条回答
  • 2021-01-15 23:13

    The issue you're facing is because some of the items in your data are duplicated. To solve it, you need to use drop_duplicates in the relevant places:

    df["color"] = "blue"
    df.loc[df.Src.isin(["x.serm.cool", "cdc.fre.gh"]), "color"] = "green"
    df["Dst"] = df.Dst.apply(lambda x: x[1:-1].split(","))
    df = df.explode("Dst").drop_duplicates()
    G = nx.from_pandas_edgelist(df, 'Src', 'Dst')
    
    colors = df[["Src", "color"]].drop_duplicates()["color"]
    nx.draw(G, node_color = colors)
    

    The output:

    0 讨论(0)
提交回复
热议问题