Python 如何在数据清理过程中有效地删除所有数字?
这是我的数据集Python 如何在数据清理过程中有效地删除所有数字?,python,regex,pandas,dataframe,Python,Regex,Pandas,Dataframe,这是我的数据集 id descriptions 0 kartu debit 20 10 indomaretcipete r 1 tarikan atm 20 10 2 tarikan atm 19 10 3
id descriptions
0 kartu debit 20 10 indomaretcipete r
1 tarikan atm 20 10
2 tarikan atm 19 10
3 biaya adm
4 trsf 18 10 wsid 23881 indah lestari
这就是我所做的
def cleaning(text):
stops = {'10', '18','19', '20', '23881'}
text = [word for word in text if not word in stops]
text = " ".join(text)
return(text)
df['description_clean'] = df['description'].apply(cleaning)
这是我得到的
id descriptions
0 kartu debit indomaretcipete r
1 tarikan atm
2 tarikan atm
3 biaya adm
4 trsf wsid indah lestari
这是无效的,我不断添加新数字以改进stopwords,如何一次完成?IIUC,您需要从数据帧中删除数字,请使用以下方法:
df_new=df.replace('\d+ ','',regex=True)
print(df_new)
id descriptions
0 0 kartu debit indomaretcipete r
1 1 tarikan atm 10
2 2 tarikan atm 10
3 3 biaya adm
4 4 trsf wsid indah lestari
仅适用于一个系列:df['descriptions']=df['descriptions']。替换('\d+','',regex=True)
注意:我在正则表达式中的
d+
后面添加了一个空格,这取决于您的示例,如果您愿意,可以不使用它 IIUC,您需要从数据帧中删除数字,请使用以下命令:
df_new=df.replace('\d+ ','',regex=True)
print(df_new)
id descriptions
0 0 kartu debit indomaretcipete r
1 1 tarikan atm 10
2 2 tarikan atm 10
3 3 biaya adm
4 4 trsf wsid indah lestari
仅适用于一个系列:df['descriptions']=df['descriptions']。替换('\d+','',regex=True)
注意:我在正则表达式中的d+
后面添加了一个空格,这取决于您的示例,如果您愿意,可以不使用它 您需要:
def replace_numbers(s):
return re.sub(r'\d*', '', s)
df['description'] = df['description'].apply(replace_numbers)
你需要:
def replace_numbers(s):
return re.sub(r'\d*', '', s)
df['description'] = df['description'].apply(replace_numbers)
使用和:
或:
或:
使用和: 或: 或:
到目前为止最好的答案,但也是slow@NabihBawazir熊猫的串操作通常很慢。检查其他解决方案是否加快了流程。目前为止,最好的答案是slow@NabihBawazir熊猫的串操作通常很慢。检查其他解决方案是否加快了流程。
df['descriptions'] = [' '.join(re.findall('[a-zA-Z]+',s)) for s in df['descriptions']]
print(df)
id descriptions
0 0 kartu debit indomaretcipete r
1 1 tarikan atm
2 2 tarikan atm
3 3 biaya adm
4 4 trsf wsid indah lestari