Skip to content
Advertisement

how to remove white space from strings of data frame column?

I am trying to loop through a column in a pandas data frame to remove unnecessary white space in the beginning and end of the strings within the column. My data frame looks like this:

JavaScript

I tried the this answer here, but did not work either. The reason I need to remove the white space from the strings in this column is that I want to one hot encode this column using get.dummies() function. My idea was to use the strip() function to remove the white space from each value and then I use .str.get_dummies(‘,’):

JavaScript

but my code returns duplicate columns and I don’t want this…I suppose the function to remove the white space is not working well? Can anyone help? My current output is:

JavaScript

columns ‘ac’ and ‘ab’ are duplicated. I want to remove the duplicated columns

Advertisement

Answer

I would stack, strip, get_dummies, and groupby.max:

If the separator is ', ':

JavaScript

else:

JavaScript

output:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement