Pandas Python Regex:错误:无需重复 [英] Pandas Python Regex : error: nothing to repeat
本文介绍了Pandas Python Regex:错误:无需重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有两个奇怪字符"*"和-"的数据框.
I have a dataframe with a couple of strange characters, "*" and "-".
import pandas as pd
import numpy as np
data = {'year': [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012],
'team': ['Bears', 'Bears', 'Bears', 'Packers', 'Packers', 'Lions', 'Lions', 'Lions'],
'wins': [11, '*', 10, '-', 11, 6, 10, 4],
'losses': [5, 8, 6, 1, 5, 10, 6, 12]}
football = pd.DataFrame(data, columns=['year', 'team', 'wins', 'losses'])
我想用'0.00'替换奇怪的字符,但出现错误-
I would like to replace the strange characters with '0.00' but I get an error -
error: nothing to repeat
我了解这与正则表达式有关,但我仍然不知道如何解决该问题.
I understand this is linked to regex but I still dont know how to overcome the issue.
我用来替换字符的代码:
the code I use to replace the characters:
football.replace(['*','-'], ['0.00','0.00'], regex=True).astype(np.float64)
推荐答案
*
是正则表达式中的特殊字符,您必须对其进行转义:
*
is a special character in regex, you have to escape it:
football.replace(['\*','-'], ['0.00','0.00'], regex=True).astype(np.float64)
或使用字符类:
football.replace([*-], '0.00', regex=True).astype(np.float64)
这篇关于Pandas Python Regex:错误:无需重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文