Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas groupby with boolean OR

I'd like to generate a set of groups based on a boolean OR criterion in pandas. A group consists of members that match on column A OR column B.

For example, in this dataframe:

df = pd.DataFrame([[1,1],[2,2],[2,3],[2,4],[3,3],[4,5]], columns = ['A','B'])

   A  B
0  1  1
1  2  2
2  2  3
3  2  4
4  3  3
5  4  5

Because rows 1, 2 and 3 match on column A, and 2 and 4 match on column B, I'd like the id values to be:

   A  B  id
0  1  1  0
1  2  2  1
2  2  3  1
3  2  4  1
4  3  3  1
5  4  5  2

I can't find any solution short of creating an NxN scipy graph with the connections, and using scipy.sparse.csgraph.connected_components. Are there any more straightforward options?

like image 591
amball Avatar asked Dec 06 '25 03:12

amball


2 Answers

Notice I think this is network problem , so we do with networkx

import networkx as nx
G=nx.from_pandas_edgelist(df, 'A', 'B')
l=list(nx.connected_components(G))
l
[{1}, {2, 3}]

from itertools import chain
l=[dict.fromkeys(y,x)for x,y in enumerate(l)]#create the list of dict for later map 
d=dict(chain(*map(dict.items,l)))# flatten the list of dict to one dict 

df['ID']=df.B.map(d)

df
   A  B  ID
0  1  1   0
1  2  2   1
2  2  3   1
3  3  3   1

Update

s1=df.A.astype('category').cat.codes.sort_values()

s2=df.B.astype('category').cat.codes.sort_values()

s=((s1==s1.shift())|(s2==s2.shift())).eq(False).cumsum()
s
#df['new']=s
Out[25]:
0    1
1    2
2    2
3    2
4    2
5    3
dtype: int32+
like image 75
BENY Avatar answered Dec 08 '25 16:12

BENY


Thanks to @W-B for putting me on the right lines. Here's a more general answer that works for >2 columns and where the values are not related across columns.

import pandas as pd
import networkx as nx
from itertools import chain, combinations

columns = ['A','B','C']
df = pd.DataFrame([[1,1,1],[2,2,2],[2,3,3],[2,4,4],[3,3,4],[4,5,5]], columns = columns)
# make columns unique, so that values in any column are not treated as equivalent to values in another
# if you don't want to overwrite values, create new columns instead
for col in df.columns:
    df[col] = str(col)+df[col].astype(str)

colPairs = list(combinations(columns, 2)) # we could match on a subset of column pairs instead
G = nx.compose_all([nx.from_pandas_edgelist(df, colPair[0], colPair[1]) for colPair in colPairs])
l=list(nx.connected_components(G))
l=[dict.fromkeys(y,x)for x,y in enumerate(l)]
d=dict(chain(*map(dict.items,l)))
df['ID']=df.B.map(d)

print(df)

    A   B   C  ID
0  A1  B1  C1   0
1  A2  B2  C2   1
2  A2  B3  C3   1
3  A2  B4  C4   1
4  A3  B3  C4   1
5  A4  B5  C5   2
like image 43
amball Avatar answered Dec 08 '25 16:12

amball



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!