Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

gather element that share infomation with pandas

My task is to gather element that share something in common.

Example:

pd.DataFrame({'C1': ['A', 'B', 'C', 'D', 'R', 'X'], 'C2': ['B', 'C', 'D', 'E', 'S', 'Y']})
    C1  C2
0   A   B
1   B   C
2   C   D
3   D   E
4   R   S
5   X   Y

and This is what I’m looking for:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    Groups
0   [A, B, C, D, E]
1   [R, S]
2   [X, Y]

Any Idea ?

>Solution :

You should use networkx.connected_components here.

Approaching your data as a graph is a reliable way to ensure grouping all values together.

import networkx as nx

G = nx.from_pandas_edgelist(df, source='C1', target='C2')

out = pd.DataFrame({'Groups': map(sorted, nx.connected_components(G))})

output:

            Groups
0  [A, B, C, D, E]
1           [R, S]
2           [X, Y]

Your graph:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading