我有一张如下表格
endsWith
所需的输出低于
startsWith
如何获得所需的输出?查看字典方法,而不是找到我们可以按键或值分组的任何位置。
答案 0 :(得分:1)
当看到这种类型的"路径"我想到的问题"图表"。 Networkx是一个处理图形网络的python库。让我们使用networkx:
for()
输出:
#Import libraries
import networkx as nx
import pandas as pd
from io import StringIO
#Read in data into a pandas dataframe
txt = StringIO("""ID|Identifier1|Identifier2
1|a|c
2|b|f
3|a|g
4|c|h
5|b|j
6|d|f
7|e|k
8|i|
9|l|h""")
df = pd.read_csv(txt,sep='|')
#Create Graph network using networkx
G = nx.from_pandas_dataframe(df,source='Identifier1',target='Identifier2')
#Create output dataframe using graph `nodes` and `node_connected_component`
df1 = pd.DataFrame({'Identifier':sorted(G.nodes())})\
.apply(lambda x: pd.Series([x.Identifier,
sorted(list(nx.node_connected_component(G,x.Identifier)))]), axis=1)
df1.columns = ['Identifier','Gr.Members']
#Use pd.factorize to create unique ids for each group of connected components
df1['Gr_ID'] = pd.factorize(df1['Gr.Members'].apply(tuple))[0] + 1
df1