跳转至

🔥AI副业赚钱星球

点击下面图片查看

郭震AI

小技巧16:如何使用 apply 去掉特殊字符?

某列单元格含有特殊字符,如标点符号,使用元素级操作方法 apply 干掉它们:

import string
exclude = set(string.punctuation)

def remove_punctuation(x):
    x = ''.join(ch for ch in x if ch not in exclude)
    return x
# 原df
Out[26]: 
      a       b
0   c,d  edc.rc
1     3       3
2  d ef       4

# 过滤a列标点
In [27]: df.a = df.a.apply(remove_punctuation) 
In [28]: df                
Out[28]: 
      a       b
0    cd  edc.rc
1     3       3
2  d ef       4

大家在看

京ICP备20031037号-1 | AI之家 | AI资讯 | Python200 | 数据分析