gpt4 book ai didi

python - 从 pandas 列中删除非 ASCII 字符

转载 作者:太空狗 更新时间:2023-10-29 19:33:36 25 4
gpt4 key购买 nike

一段时间以来,我一直在努力解决这个问题。我试图从 DB_user 列中删除非 ASCII 字符,并尝试用空格替换它们。但我不断收到一些错误。这是我的数据框的样子:

+-----------------------------------------------------------|      DB_user                            source   count  |                                             +-----------------------------------------------------------| ???/"Ò|Z?)?]??C %??J                      A        10   |                                       | ?D$ZGU   ;@D??_???T(?)                    B         3   |                                       | ?Q`H??M'?Y??KTK$?Ù‹???ЩJL4??*?_??        C         2   |                                        +-----------------------------------------------------------

I was using this function, which I had come across while researching the problem on SO.

def filter_func(string):
for i in range(0,len(string)):


if (ord(string[i])< 32 or ord(string[i])>126
break

return ''

And then using the apply function:

df['DB_user'] = df.apply(filter_func,axis=1)

我不断收到错误:

'ord() expected a character, but string of length 66 found', u'occurred at index 2'

但是,我认为通过使用 filter_func 函数中的循环,我通过将一个字符输入到“ord”中来处理这个问题。因此,当它遇到非 ASCII 字符时,应将其替换为空格。

有人能帮帮我吗?

谢谢!

最佳答案

你可以试试这个:

df.DB_user.replace({r'[^\x00-\x7F]+':''}, regex=True, inplace=True)

关于python - 从 pandas 列中删除非 ASCII 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36340627/

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