从 pandas 数据框的一列以上构建 networkx 有向图或流程图
Build networkx directed graph or flow chart from more than one column of pandas dataframe
我有 pandas 数据框,其中包含 10 列。
- 每行包含一个用户上线的步骤。总共有 10 列,所以所有 10 个步骤过程
- 让我们先说 activity 正在预订机票所以步骤是
登录网站-->give src dest time-->select seats-->pay--review
所以每一步都可能发生各种排列,我想从所有数据集中绘制一个有向图。
当前 networkx 仅支持
中的 2 列
# libraries
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
# Build your graph
G=nx.from_pandas_dataframe(df, 'src', 'dest',create_using=nx.DiGraph())
# Plot it
nx.draw(G, with_labels=True)
plt.show()
谁能告诉我如何为多于两列的有向图创建它
networkx from_pandas_dataframe
使用add_edges_from
,你可以做类似的事情:
# libraries
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
# Build your graph
df = pd.DataFrame(np.random.randn(2,4),columns=list('ABCD')) #Create a 4 column data frame
columns = list(df.columns.values)# Get columns name
g = nx.empty_graph(0, nx.DiGraph()) #initialize an empty graph
for i in range(len(columns)-1):
g.add_edges_from(zip(df[columns[i]], df[columns[i+1]])) #Create edge between 2 values, between all consecutive coumns
# Plot it
nx.draw(g, with_labels=True)
plt.show()
结果:
我有 pandas 数据框,其中包含 10 列。
- 每行包含一个用户上线的步骤。总共有 10 列,所以所有 10 个步骤过程
- 让我们先说 activity 正在预订机票所以步骤是 登录网站-->give src dest time-->select seats-->pay--review
所以每一步都可能发生各种排列,我想从所有数据集中绘制一个有向图。
当前 networkx 仅支持
中的 2 列# libraries
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
# Build your graph
G=nx.from_pandas_dataframe(df, 'src', 'dest',create_using=nx.DiGraph())
# Plot it
nx.draw(G, with_labels=True)
plt.show()
谁能告诉我如何为多于两列的有向图创建它
networkx from_pandas_dataframe
使用add_edges_from
,你可以做类似的事情:
# libraries
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
# Build your graph
df = pd.DataFrame(np.random.randn(2,4),columns=list('ABCD')) #Create a 4 column data frame
columns = list(df.columns.values)# Get columns name
g = nx.empty_graph(0, nx.DiGraph()) #initialize an empty graph
for i in range(len(columns)-1):
g.add_edges_from(zip(df[columns[i]], df[columns[i+1]])) #Create edge between 2 values, between all consecutive coumns
# Plot it
nx.draw(g, with_labels=True)
plt.show()
结果: