一次替换 pandas 列中的多个值 [英] Replacing multiple values in pandas column at once

查看:45
本文介绍了一次替换 pandas 列中的多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常基本的问题,但在阅读了 Stack 和 str.replace 的文档后,我没有找到答案.试图删除列中的所有标点符号.什么是正确的语法?

This is an incredibly basic question but after reading through Stack and the documentation for str.replace, I'm not finding the answer. Trying to drop all of the punctuation in a column. What would be the proper syntax for this?

这可行,但很荒谬:igog['text']=igog['text'].str.replace(",","").str.replace("/","").str.replace(".","").str.replace("\"","").str.replace("?","").str.replace(";","")

这不会:igog['text'] = igog['text'].str.replace({",","","/","",".",",""",""""""",""""}).

因为我不断收到replace() 缺少 1 个必需的位置参数:'repl'".

Because I keep getting "replace() missing 1 required positional argument: 'repl'".

提前致谢!

推荐答案

你可以做一个这样的简单循环:

You can make a simple loop like this:

t=",/.\?;"
for i in t:
   igog[text]=igog[text].replace(i,"")

或者你可以使用regex:

igog['text'].str.replace("[,/.\?;]", "")

或者你可以使用 re.sub():

import re
igog['text'] = re.sub('[,/.\?;]', "", igog['text'])

或者你可以定义一个翻译表:

igog['text'].translate({ord(ch):' ' for ch in ',/.\?;'})

这篇关于一次替换 pandas 列中的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆