0

I have a csv file that I want to make a network out of.

The csv file I have looks something like this

Company Investors
(comp1) (investorA)
(comp2) (investorB)
(comp3) (investorC)
(comp4) (investorB, investorC)

I am using pandas and networkx on python.

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.

I am very new to coding. Any help would be appreciated.

0

1 Answer 1

1

Use 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

df['Investors'] = df['Investors'].str.strip('\[|\]').str.split(',')
df_e = df.explode('Investors')
nx.from_pandas_edgelist(df_e, 'Company', 'Investors')
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.