[英]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) ![]() |
(investorA) ![]() |
(comp2) ![]() |
(investorB) ![]() |
(comp3) ![]() |
(investorC) ![]() |
(comp4) ![]() |
(investorB, investorC) ![]() |
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.