Home > Back-end >  Count values that go in row in group?
Count values that go in row in group?

Time:11-11

I have two dataframes:

id     val
a      w
a      w
a      l
a      w
b      w
b      w
b      w
c      w
c      l
d      w
d      w
d      w
d      w

I want to get ids which had 3 w in row in column val. So desired result must be:

id 
b      
d      

As you see only ids b and d had at least 3 w in column val in row. How to do that?

CodePudding user response:

Not the fastest solution but here:

import pandas as pd
import re # regular expression matching

df = <your_dataframe>

ids = [i for i in df.id.unique() if re.search('w{3}', ''.join(df[df.id==i].val))]

w{3} means look for 3 consecutive 'w's

  • Related