使用 Canopy 和 Pandas,我有数据框 a ,其定义如下:
a=pd.read_csv('text.txt')
df=pd.DataFrame(a)
df.columns=["test"]
test.txt 是一个单列文件,其中包含包含文本、数字和标点符号的字符串列表。
假设 df 看起来像:
test
%hgh&12
abc123!!!
猪肉薯条
我希望我的结果是:
test
hgh12
abc123
猪肉薯条
迄今为止的努力:
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
上面的命令基本上只是返回相同的数据集。
感谢任何线索。
编辑:我使用 Pandas 的原因是因为数据很大,大约有 1M 行,并且未来编码的使用将应用于最多 30M 行的列表。
长话短说,我需要以非常有效的方式清理大数据集的数据。
Use replace
使用正确的正则表达式会更容易:
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]
将正则表达式与模式一起使用,这意味着不是字母数字/空格
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]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)