Pandas groupby with boolean OR

Viewed 384

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?

3 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+

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

We can do this using the Counter class. We count the number of occurrences for every element in the column and create a temporary column with those values. If the value for a row in that temporary column is greater than 1 (which means that number occurs more than once, we change the id column.

import pandas as pd
from collections import Counter as ctr
df = pd.DataFrame([[1,1],[2,2],[2,3],[2,4],[3,3],[4,5]], columns = ['A','B'])
df['id'] = 0
for i in range(len(df.columns)):
  if list(df.columns)[i] != 'id':
    c = dict(ctr(df[list(df.columns)[i]]))
    df[list(df.columns)[i] + '_1'] = df[list(df.columns)[i]].apply(lambda x: c[x])
    df.loc[df[list(df.columns)[i] + '_1'] > 1, 'id'] = 1
    df = df.drop(columns=[list(df.columns)[i] + '_1'])

df
   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   0

This one should be scalable for >2 columns.

Related