简体   繁体   English

Networkx:来自 pandas dataframe 的网络图

[英]Networkx: Network graph from pandas dataframe

I have the following dataset:我有以下数据集:

Company_ID  Firm_Name
125911      Ampersand 
125911      BancBoston 
32679       BP Corp 
74240       CORNING 
32679       DIEBOLD 
32679       DIEBOLD 
74240       Fidelity 
74240       Greylock
32679       INCO 
67734       INCO 
67734       Innova
32679       Kleiner 
67734       Kleiner 
67734       Kleiner 
67734       Mayfield
32679       Pliant 
67734       Pliant 
67734       Sofinnova 
43805       Warburg 

The dataframe shows when different investment firms have invested in the same Company during a year. dataframe 显示不同投资公司在一年内投资同一家公司的时间。 I want to create a network graph of the Connections between the Firm_ID only.我只想创建 Firm_ID 之间的连接的网络图。 For example Ampersand and BancBoston have both invested in the same company and should therefore be connected.例如,Ampersand 和 BancBoston 都投资了同一家公司,因此应该建立联系。 The code I have tried is:我试过的代码是:

G = nx.Graph()
G = nx.from_pandas_edgelist(df, 'Company_ID', 'Firm_Name')
nx.draw_shell(H, with_labels=True)

Which generates the following graph:这会生成以下图表: 在此处输入图像描述

This shows the connections of both Company_ID and Firm_Name.这显示了 Company_ID 和 Firm_Name 的连接。 I only want to have the Firms as nodes, where they are connected if they have invested in the same company.我只想将这些公司作为节点,如果他们投资了同一家公司,它们就会连接起来。 I have not found any similar problems or similar datasets where networkx is used.我没有发现任何使用 networkx 的类似问题或类似数据集。 Any help is greatly appreciated!任何帮助是极大的赞赏!

Try with merge尝试merge

out = df.merge(df,on=['Company_ID'])
G = nx.Graph()
G = nx.from_pandas_edgelist(df, 'Firm_Name_x', 'Firm_Name_y')

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

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