gpt4 book ai didi

python-3.x - 从 Pandas 系列中的字符串列表中删除空格

转载 作者:行者123 更新时间:2023-12-04 04:16:03 25 4
gpt4 key购买 nike

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

我想从列表中的每个元素中删除前导和尾随空格。

我正在尝试这个:

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

最佳答案

使用 list comprehensionstrip在列表中:

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

关于python-3.x - 从 Pandas 系列中的字符串列表中删除空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60684938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com