How to remove string value from column in pandas dataframe

I am trying to write code that breaks a row in a dataframe column into a comma (so it becomes a list) and removes a specific row from this list if it is present. after deleting an unnecessary line, I want to join the list items again with a comma. My DataFrame is as follows:

df: Column1 Column2 0 aa,b,c 1 yb,n,m 2 dn,n,m 3 db,b,x 

So basically my goal is to remove all b values ​​from column2 to get:

DF:

  Column1 Column2 0 aa,c 1 yn,m 2 dn,n,m 3 dx 

The code I wrote is as follows:

 df=df['Column2'].apply(lambda x: x.split(',')) def exclude_b(df): for index, liste in df['column2].iteritems(): if 'b' in liste: liste.remove('b') return liste else: return liste 

The first row breaks all the values ​​in the column into a comma-separated list. using the function now, I tried to iterate through all the lists and remove b if it is, if it is missing, return the list as is. If I print 'liste' at the end, it returns only the first row of Column2, but not the rest. What am I doing wrong? And will there be a way to implement my if condition into a lambda function?

+5
source share
1 answer

just you can apply regular expression b,? , which means replacing any value of b and, found after b if exists

 df['Column2'] = df.Column2.str.replace('b,?' , '') Out[238]: Column1 Column2 0 aa,c 1 yn,m 2 dn,n,m 3 dx 
+8
source

Source: https://habr.com/ru/post/1234781/


All Articles