根据标点符号列表替换数据框中的标点符号

编程入门 行业动态 更新时间:2024-10-15 12:36:27
本文介绍了根据标点符号列表替换数据框中的标点符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用冠层和熊猫,我有一个数据框,其定义为:

Using Canopy and Pandas, I have data frame a which is defined by:

a=pd.read_csv('text.txt') df=pd.DataFrame(a) df.columns=["test"]

test.txt是一个单列文件,其中包含一个包含文本,数字和标点符号的字符串列表.

test.txt is a single column file that contains a list of string that contains text, numerical and punctuation.

假设df看起来像:

测试

%hgh& 12

%hgh&12

abc123 !!!

abc123!!!

炸薯条

我希望我的结果是:

I want my results to be:

测试

hgh12

abc123

炸薯条

到目前为止的努力:

Effort so far:

from string import punctuation /-- import punctuation list from python itself a=pd.read_csv('text.txt') df=pd.DataFrame(a) df.columns=["test"] /-- define the dataframe for p in list(punctuation): ...: df2=df.med.str.replace(p,'') ...: df2=pd.DataFrame(df2); ...: df2

上面的命令基本上只是给我返回相同的数据集. 感谢任何潜在客户.

The command above basically just returns me with the same data set. Appreciate any leads.

之所以使用Pandas,是因为数据量巨大,跨越了大约100万行,并且将来使用的编码方式将应用于多达3000万行的列表. 长话短说,我需要以非常有效的方式清理大数据集的数据.

Reason why I am using Pandas is because data is huge, spanning to bout 1M rows, and future usage of the coding will be applied to list that go up to 30M rows. Long story short, I need to clean data in a very efficient manner for big data sets.

推荐答案

在正确的正则表达式中使用replace会更容易:

Use replace with correct regex would be easier:

In [41]: import pandas as pd pd.set_option('display.notebook_repr_html', False) df = pd.DataFrame({'text':['test','%hgh&12','abc123!!!','porkyfries']}) df Out[41]: text 0 test 1 %hgh&12 2 abc123!!! 3 porkyfries [4 rows x 1 columns]

使用正则表达式的模式表示不使用字母数字/空格

use regex with the pattern which means not alphanumeric/whitespace

In [49]: df['text'] = df['text'].str.replace('[^\w\s]','') df Out[49]: text 0 test 1 hgh12 2 abc123 3 porkyfries [4 rows x 1 columns]

更多推荐

根据标点符号列表替换数据框中的标点符号

本文发布于:2023-10-08 15:13:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472999.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标点符号   框中   数据   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!