简体   繁体   English

从 csv 文件(Python、Pandas、Networkx)创建网络

[英]Creating network from csv file (Python, Pandas, Networkx)

I have a csv file that I want to make a network out of.我有一个 csv 文件,我想用它建立一个网络。

The csv file I have looks something like this我拥有的 csv 文件看起来像这样

Company公司 Investors投资者
(comp1) (comp1) (investorA) (投资者A)
(comp2) (comp2) (investorB) (投资者B)
(comp3) (comp3) (investorC) (投资者C)
(comp4) (comp4) (investorB, investorC) (投资者B,投资者C)

I am using pandas and networkx on python.我在 python 上使用 pandas 和 networkx。

My network's nodes will be each companies and investors.我的网络节点将是每个公司和投资者。

I think I can make it work if each row had only one investor (like comp1~3) But, I am having trouble with rows with multiple investors.我想如果每一行只有一个投资者(如 comp1~3)我可以让它工作但是,我在有多个投资者的行时遇到了麻烦。

I am very new to coding.我对编码很陌生。 Any help would be appreciated.任何帮助,将不胜感激。

Use pd.DataFrame.explode :使用pd.DataFrame.explode

df_e = df.explode('Investors')
nx.from_pandas_edgelist(df_e, 'Company', 'Investors')

If Investors is a string and not a tuple, you can use the following如果 Investors 是字符串而不是元组,则可以使用以下内容

df['Investors'] = df['Investors'].str.strip('\[|\]').str.split(',')
df_e = df.explode('Investors')
nx.from_pandas_edgelist(df_e, 'Company', 'Investors')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM