MarkS
MarkS

Reputation: 1539

Remove whitespace from list of strings in a pandas series

I have a dataframe in which one columns values are lists of strings.

I want to remove the leading and trailing white space from each of the elements in the lists.

I am trying this:

interests_no_nulls = fcc['JobRoleInterest'].dropna()
splitted_interests = interests_no_nulls.str.split(',')
splitted_interests.apply(lambda x: x.strip())

But I get this:

Traceback (most recent call last):
  File "C:\Users\Mark\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\series.py", line 4045, in apply
    mapped = lib.map_infer(values, f, convert=convert_dtype)
  File "pandas/_libs/lib.pyx", line 2228, in pandas._libs.lib.map_infer
  File "C:/Users/Mark/PycharmProjects/main/main.py", line 60, in <lambda>
    splitted_interests.apply(lambda x: x.strip())
AttributeError: 'list' object has no attribute 'strip'

Notice the leading or trailing white space in some of the list elements: enter image description here

Upvotes: 4

Views: 1571

Answers (1)

jezrael
jezrael

Reputation: 863291

Use list comprehension for strip in lists:

fcc = pd.DataFrame({'JobRoleInterest':['aa,ss','dd , ff','k ,dd', 'j, gg']})
interests_no_nulls = fcc['JobRoleInterest'].dropna()
splitted_interests = interests_no_nulls.str.split(',')
print (splitted_interests.apply(lambda x: [y.strip() for y in x]))
0    [aa, ss]
1    [dd, ff]
2     [k, dd]
3     [j, gg]
Name: JobRoleInterest, dtype: object

Or change separator by \s*,\s* - \s* is for zero or more whitespaces:

fcc = pd.DataFrame({'JobRoleInterest':['aa,ss','dd , ff','k ,dd', 'j, gg']})
interests_no_nulls = fcc['JobRoleInterest'].dropna()
splitted_interests = interests_no_nulls.str.split('\s*,\s*')
print (splitted_interests)
0    [aa, ss]
1    [dd, ff]
2     [k, dd]
3     [j, gg]
Name: JobRoleInterest, dtype: object

Upvotes: 2

Related Questions