从熊猫系列的字符串列表中删除空格

时间:2020-03-14 16:32:02

标签: python-3.x pandas

我有一个数据框,其中一列的值是字符串列表。

我想从列表中的每个元素中删除开头和结尾的空白。

我正在尝试:

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

但是我明白了:

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'

请注意某些列表元素中的前导或尾随空格: enter image description here

1 个答案:

答案 0 :(得分:1)

在列表中的list comprehension中使用strip

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

或将分隔符更改\s*,\s*-\s*用于零个或多个空格:

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