Skip to content Skip to sidebar Skip to footer

Mapping Matching Word Count On A Column Using Pandas In Python

I have a df, Name Step Description Ram 1 Ram is oNe of the good cricketer Ram 2 gopal one Sri 1 Sri is one of the member Sri 2 ra

Solution 1:

Create new mask and apply it:

my_list=["one","good"]

mask=df["Description"].str.contains("|".join(my_list),na=False,flags=re.IGNORECASE ) & \
     (df.groupby('Name').cumcount() == 0)
print (mask)
0      True
1     False
2      True
3     False
4     False
5      True
6      True
7      True
8      True
9      True
10     True
11     True
12     True
dtype: bool

extracted = df['Description'].str.findall('(' + '|'.join(my_list) + ')', flags=re.IGNORECASE)
df.loc[mask, 'keys'] = extracted.str.join(',')
df.loc[mask, 'count'] = extracted.str.len()
print (df)
       Name  Step                       Description      keys  count
0       Ram     1  Ram is oNe of the good cricketer  oNe,good    2.0
1       Ram     2                         gopal one       NaN    NaN
2       Sri     1          Sri is one of the member       one    1.0
3       Sri     2                        ravi good        NaN    NaN
4     Kumar     1                 Kumar is a keeper       NaN    NaN
5     Madhu     1                          good boy      good    1.0
6   Vignesh     1                        oNe little       oNe    1.0
7     Pechi     1                          one book       one    1.0
8     mario     1                      good randokm      good    1.0
9     Roger     1                   one milita good  one,good    2.0
10     bala     1                        looks good      good    1.0
11      raj     1                          more one       one    1.0
12     venk     1                        likes good      good    1.0

EDIT:

#transform all values if need same size of original
s = df.groupby('Name')['Description'].transform(','.join)
print (s)
0     Ram is oNe of the good cricketer,gopal one
1     Ram is oNe of the good cricketer,gopal one
2            Sri is one of the member,ravi good 
3            Sri is one of the member,ravi good 
4                              Kumar is a keeper
5                                       good boy
6                                     oNe little
7                                       one book
8                              good randokm good
9                                one milita good
10                                    looks good
11                                      more one
12                                    likes good
Name: Description, dtype: object

#for mask use new Series s
mask=s.str.contains("|".join(my_list),na=False,flags=re.IGNORECASE ) & \
     (df.groupby('Name').cumcount() == 0)
print (mask)
0      True
1     False
2      True
3     False
4     False
5      True
6      True
7      True
8      True
9      True
10     True
11     True
12     True
dtype: bool

#extract from new Series s
extracted = s.str.findall('(' + '|'.join(my_list) + ')', flags=re.IGNORECASE).apply(set)
df.loc[mask, 'keys'] = extracted.str.join(',')
df.loc[mask, 'count'] = extracted.str.len()
print (df)
       Name  Step                       Description          keys  count
0       Ram     1  Ram is oNe of the good cricketer  good,oNe,one    3.0
1       Ram     2                         gopal one           NaN    NaN
2       Sri     1          Sri is one of the member      good,one    2.0
3       Sri     2                        ravi good            NaN    NaN
4     Kumar     1                 Kumar is a keeper           NaN    NaN
5     Madhu     1                          good boy          good    1.0
6   Vignesh     1                        oNe little           oNe    1.0
7     Pechi     1                          one book           one    1.0
8     mario     1                 good randokm good          good    1.0
9     Roger     1                   one milita good      good,one    2.0
10     bala     1                        looks good          good    1.0
11      raj     1                          more one           one    1.0
12     venk     1                        likes good          good    1.0

Post a Comment for "Mapping Matching Word Count On A Column Using Pandas In Python"