如何从数据框中的特定列中删除特定的标点符号 [不是所有标点符号]?

问题描述

我是堆栈溢出的新手,遇到了一些麻烦。

我尝试使用此代码一个字符串执行我想要的操作,但是当我尝试从代码中创建一个函数并将其应用于我的数据框中的整个列时它不起作用 [列条目仅显示运行此代码显示为“无”,而不是显示删除标点符号的原始文本]。我真的被困住了,直到我弄清楚这一点才能够继续这个项目。

我将上传我的代码的一些屏幕截图,并将代码放在正文中。谢谢!!编辑:屏幕截图包括我的代码输出

Edit2:我还尝试将“teststr”重置为等于 df.lower_postsnostopwords2 [我正在尝试编辑的列的名称],但这也不起作用。

screenshot 1screenshot 2

screenshot 3

代码如下:

test_str = "hi !" 
punc = '''!()-[]{};:'"\,<>/@#$%^&*_~''' 
    
    for ele in test_str: 
    if ele in punc:  
        test_str = test_str.replace(ele,"") 

print (test_str)

def removepunc(test_str):
    for ele in test_str:  
        if ele in punc:  
            test_str = test_str.replace(ele,"")


df['newcolumn'] = df['lower_postsnostopwords2'].apply(removepunc) 
#trying to apply this to a specific column in my dataframe`

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)