Используя unique
, переключите столбец с 0 на 1 и столбец с 1 на 0 concat
оба df вместе
adj=pd.DataFrame(df.groupby('place').id.unique().loc[lambda x : x.str.len()>1].tolist())
pd.concat([adj,adj.rename(columns={0:1,1:0})])
Out[810]:
0 1
0 1 2
0 2 1
Обновление:
newdf=df.merge(df,on='place')
x=nx.from_pandas_dataframe(newdf,'id_x','id_y') # using merge to get the connect for all id by link columns place.
[list(itertools.permutations(x, len(x)) for x in list(nx.connected_components(x))] # using permutations get the all combination for each connected_components in networkx
Out[821]: [[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]]
Ввод данных
df
Out[822]:
id place
0 1 bar
1 1 pool
2 2 bar
3 3 bar