負荷にデータをにパンダを必要としますグラフ。例えば、networkx、
G = nx.from_pandas_dataframe(df, 'email', 'phone', 'index')
を使用して各email
及びデータフレームに記載されているphone
、エッジの属性として関連付けられたインデックスとの間のエッジを持つグラフを作成します。
そして個人がグラフのconnected componentsとして同定することができる。
components = nx.connected_component_subgraphs(G)
次に、各成分のエッジに関連するインデックス値を見つけることができます。 インデックスは、次にように、あなたに一人の個人に関連付けられているすべての行を示し、元のデータフレームをスライスするために使用することができます。
import pandas as pd
import networkx as nx
df = pd.DataFrame({'email': ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]'],
'phone': [123, 123, 456, 333, 443], 'index': [0, 1, 2, 3, 4]})
G = nx.from_pandas_dataframe(df, 'email', 'phone', 'index')
components = nx.connected_component_subgraphs(G)
for cc in components:
idx = [dct['index'] for node1, node2, dct in cc.edges(data=True)]
group = df.iloc[idx]
print(group)
print('-'*80)
利回り
email index phone
2 [email protected] 2 456
4 [email protected] 4 443
--------------------------------------------------------------------------------
email index phone
0 [email protected] 0 123
1 [email protected] 1 123
3 [email protected] 3 333
示す例と一緒にいくつかのサンプルデータを入力してください期待される出力。 –
あなたの意図を理解し、助けてくれるサンプルと対応する出力を提供してください –
いいえ、Pandas groupbyは適切なツールではありません。データをグラフで表現し、[connected components](https://en.wikipedia.org/wiki/Connected_component_(graph_theory))を見つける必要があります。 – unutbu