我的数据帧有数千行。
看起来像这样:
import pandas as pd
import numpy as np
text = ['please send us a dm...','…could you please dm me','dm me plz…','i dmed u yesterday…','dm me asap thx', 'i send a dm to u now', 'thx u r so nice dming u now', 'just sent u a dm']
df = pd.DataFrame({"text": text})
text
0 please send us a dm...
1 …could you please dm me
2 dm me plz…
3 i dmed u yesterday…
4 dm me asap thx
5 i send a dm to u now
6 thx u r so nice dming u now
7 just sent u a dm
我写了一个函数来替换“text”列中的缩写。在
^{pr2}$但是,我的代码不能正常工作,所以不能完全替换数据帧中的所有缩写。在
convert(df, 'text')
text
0 please send us a dm
1 could you please direct message me
2 dm me plz
3 i direct message you yesterday
4 dm me as soon as possible thx
5 i send a direct message to you now
6 thx you r so nice direct message you now
7 just sent you a dm
期望的最终输出如下所示:
text
0 please send us a direct message
1 could you please direct message me
2 direct message me plz
3 i direct message you yesterday
4 direct message me as soon as possible thanks
5 i send a direct message to you now
6 thanks you r so nice direct message you now
7 just sent you a direct message
我不明白为什么我的代码不起作用。在
首先构建一个替换dict:
然后使用apply函数将缩写替换为所需的单词。最后将单词合并成一个完整的字符串。在
^{pr2}$看看这里
你用“dm”在“from”和“back”的空格
但在列表中,例如:
^{pr2}$在“dm”后面有一个句号,所以它不能代替它
一种更简洁的方法是使用“replacement”字典并在df和replacement dictionary中循环。如下所示:
尽管列出替代品的所有变体是一件痛苦的事,例如“dm”“dm”“dm”“plz”“plz…”等等。。。在
你可能需要研究一下正则表达式的魔力:)
相关问题 更多 >
编程相关推荐