This is not a duplicate of:
Fastest way to perform complex search on pandas dataframe
Note: pandas ver 0.23.4
Assumptions: data can be laid out in any o
You could define a graph using the values from both columns as edges, and look for the connected_components. Here's a way using NetworkX
:
import networkx as nx
G=nx.Graph()
G.add_edges_from(df.values.tolist())
cc = list(nx.connected_components(G))
# [{'A', 'B', 'C', 'D'}, {'L', 'M', 'N', 'O'}]
Now say for instance you want to filter by D
, you could then do:
component = next(i for i in cc if 'B' in i)
# {'A', 'B', 'C', 'D'}
And index the dataframe where the values from both columns are in component
:
df[df.isin(component).all(1)]
Col1 Col2
0 A B
1 B C
2 D C
The above can be extended to all items in the list, by generating a list of dataframes. Then we simply have to index using the position in which a given item is present in L
:
L = ['A', 'B', 'C', 'D', 'L', 'M', 'N', 'O']
dfs = [df[df.isin(i).all(1)] for j in L for i in cc if j in i]
print(dfs[L.index('D')])
Col1 Col2
0 A B
1 B C
2 D C
print(dfs[L.index('L')])
Col1 Col2
3 L M
4 M N
5 N O