【发布时间】:2021-11-04 10:14:38
【问题描述】:
我正在使用 Python 中的正则表达式来处理以下数据。
Random
0 helloooo
1 hahaha
2 kebab
3 shsh
4 title
5 miss
6 were
7 laptop
8 welcome
9 pencil
我想删除具有重复字母模式的单词(例如 blaaaa)、重复的一对字母(例如 hahaha)以及一个字母周围具有相同相邻字母的任何单词(例如title,kebab,were)。
代码如下:
import pandas as pd
data = {'Random' : ['helloooo', 'hahaha', 'kebab', 'shsh', 'title', 'miss', 'were', 'laptop', 'welcome', 'pencil']}
df = pd.DataFrame(data)
df = df.loc[~df.agg(lambda x: x.str.contains(r"([a-z])+\1{1,}\b"), axis=1).any(1)].reset_index(drop=True)
print(df)
下面是上面的输出,带有警告消息:
UserWarning: This pattern has match groups. To actually get the groups, use str.extract.
Random
0 hahaha
1 kebab
2 shsh
3 title
4 were
5 laptop
6 welcome
7 pencil
但是,我希望看到这个:
Random
0 laptop
1 welcome
2 pencil
【问题讨论】:
标签: python regex pandas dataframe