Skip to content Skip to sidebar Skip to footer

How To Split Values In A Datacolumn And Adding It To A New Column With A Condition In Pandas

I have a df, name Value Sri is a cricketer Sri,is Ram player Ram Ravi is a singer is cricket and foot is ball and,is,

Solution 1:

Use str.findall with str.join:

my_list=["is", "foot"]
df['my_list'] = df['Value'].str.findall('(' + '|'.join(my_list) + ')').str.join(',')
print (df)
                       name        Value  my_list
0        Sri is a cricketer       Sri,isis1                Ram player          Ram         
2          Ravi is a singer           isis3  cricket and foot is ball  and,is,foot  is,foot

Another solution with split and get intersections of sets:

my_list=["is", "foot"]
df['my_list']=df['Value'].str.split(',').apply(lambda x: set(x) & set(my_list)).str.join(',')
print (df)
                       name        Value  my_list
0        Sri is a cricketer       Sri,isis1                Ram player          Ram         
2          Ravi is a singer           isis3  cricket and foot is ball  and,is,foot  is,foot

And last:

df['Value'] = (df['Value'].str.replace('(' + '|,'.join(my_list) + ')', '')
                          .str.replace('[,]{2,}',',')
                          .str.strip(','))
print (df)
                       name Value  my_list
0        Sri is a cricketer   Sri       is1                Ram player   Ram         
2          Ravi is a singer             is3  cricket and foot is ball   andis,foot

Or:

my_list=["is", "foot"]

s1 = df['Value'].str.split(',')

df['my_list'] = s1.apply(lambda x: set(x) & set(my_list)).str.join(',')
df['Value'] = s1.apply(lambda x: set(x) - set(my_list)).str.join(',')
print (df)

                       name Value  my_list
0        Sri is a cricketer   Sri       is1                Ram player   Ram         
2          Ravi is a singer             is3  cricket and foot is ball   andis,foot

Post a Comment for "How To Split Values In A Datacolumn And Adding It To A New Column With A Condition In Pandas"