根据正则表达式字典填充Pandas DataFrame列 [英] Populating Pandas DataFrame column based on dictionary of regex
问题描述
我有一个如下数据框:
GE GO
1 AD Weiss
2 KI Ruby
3 OH Port
4 ER Rose
5 KI Rose
6 JJ Weiss
7 OH 7UP
8 AD 7UP
9 OP Coke
10 JJ Stout
,并且我尝试根据列GO
的值再添加一列.我当时正在考虑使用字典,但是我需要使用正则表达式来识别部分真实情况.例如:
and I'm trying to add one more column based on the value of of column GO
. I was thinking about using a dictionary, but I need to use regex to identify partial matches in my real case. For instance:
Dic={'Weiss|\wuby|Sto\w+':'Beer', 'Port|Rose':'Wine','\dUP|Coke':'Soda'}
这会给
GE GO OUT
1 AD Weiss Beer
2 KI Ruby Beer
3 OH Port Wine
4 ER Rose Wine
5 KI Rose Wine
6 JJ Weiss Beer
7 OH 7UP Soda
8 AD 7UP Soda
9 OP Coke Soda
10 JJ Stout Beer
lambda函数在这里可以工作吗?我将如何使其成为正则表达式?预先感谢!
Would a lambda function work here? How would I make it into regex? Thanks in advance!
推荐答案
您可以这样做:
In [253]: df['OUT'] = df[['GO']].replace({'GO':Dic}, regex=True)
In [254]: df
Out[254]:
GE GO OUT
1 AD Weiss Beer
2 KI Ruby Beer
3 OH Port Wine
4 ER Rose Wine
5 KI Rose Wine
6 JJ Weiss Beer
7 OH 7UP Soda
8 AD 7UP Soda
9 OP Coke Soda
10 JJ Stout Beer
有趣的观察-在较早的Pandas版本中,与DataFrame.replace()
和Series.str.replace()
方法相比,Series.map()
方法几乎总是更快.在Pandas 0.19.2中变得更好:
Intereseting observation - in older Pandas versions, Series.map()
method was almost always faster compared to DataFrame.replace()
and Series.str.replace()
methods. It got better in Pandas 0.19.2:
In [267]: df = pd.concat([df] * 10**4, ignore_index=True)
In [268]: %timeit df.GO.map(lambda x: next(Dic[k] for k in Dic if re.search(k, x)))
1 loop, best of 3: 1.57 s per loop
In [269]: %timeit df[['GO']].replace({'GO':Dic}, regex=True)
1 loop, best of 3: 895 ms per loop
In [270]: %timeit df.GO.replace(Dic, regex=True)
1 loop, best of 3: 876 ms per loop
In [271]: df.shape
Out[271]: (100000, 2)
这篇关于根据正则表达式字典填充Pandas DataFrame列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!