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?
source share