I have a dataframe like the following:
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
的值添加一列。我考虑使用字典,但在实际情况下,我需要使用正则表达式来识别部分匹配。例如:
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函数吗?如何将它转换为正则表达式?提前感谢!
df.GO.replace(Dic, regex=True)
。 - piRSquareddf.GO.replace(Dic, regex=True)
发布为您自己的解决方案 - 它绝对值得! - MaxU - stand with Ukraine