Python 如何在数据框中更正拼写错误的单词?
我有一个数据框,我想在其中编辑错误的单词。首先,我删除一个单词中重复两次以上的字符,然后对其应用拼写更正。对于第一部分,我只能对字符串应用更改。我希望能够将其应用于数据帧。我该怎么做Python 如何在数据框中更正拼写错误的单词?,python,pandas,Python,Pandas,我有一个数据框,我想在其中编辑错误的单词。首先,我删除一个单词中重复两次以上的字符,然后对其应用拼写更正。对于第一部分,我只能对字符串应用更改。我希望能够将其应用于数据帧。我该怎么做 text='Aye concreeete steel and plastic housesss will keep us alll safe and flourishing ?' import re def reduce_lengthening(text): pattern = re.compi
text='Aye concreeete steel and plastic housesss will keep us alll safe and flourishing ?'
import re
def reduce_lengthening(text):
pattern = re.compile(r"(.)\1{2,}")
return pattern.sub(r"\1\1", text)
print('string is: ',reduce_lengthening(text))
输出字符串为:
Aye混凝土钢结构和塑料房屋将确保我们所有人的安全和繁荣?
如何将此函数应用于以下数据帧
text=['dear pados wali anttty , can just keep your thoughts and nose out business raising_hands thaaaank .',
'but least did not call him losers suckers , juuust was did not want the cemetery and honor them , big deal.',
'some hunters are just entitled , you are lucky have them.',
'thin corrrect time that.. only one person could save from this crisis .. correct sarthak ? ?',
'thereee also the wuhan virus. that totally different ?',
'does nooot every woman hav adam apple amp; flat hairy chest ?']
import pandas as pd
df=pd.DataFrame()
df['Text']=text
如果您已使用下面的
应用功能执行此操作
df["Text"] = df["Text"].apply(reduce_lengthening)
或者在添加此列之前(使用
df['Text']=Text),您可以像这样将每个文本元素传递给列表理解中的
reduce_lengthing',并存储结果列表
df["Text"] = [reduce_lengthening(x) for x in text]