我有一个名为'market_cap_(in_us_ $)'的列,其值如下所示:
$5.41 $18,160.50 $9,038.20 $8,614.30 $368.50 $2,603.80 $6,701.50 $8,942.40我的最终目标是能够根据特定的数值进行过滤(例如,> 2000.00)。
通过阅读本网站的其他问题,我遵循以下指示:
cleaned_data['market_cap_(in_us_$)'].replace( '$', '', regex = True ).astype(float)但是,我收到以下错误
TypeError: replace() got an unexpected keyword argument 'regex'如果我从替换参数中删除“regex = True”,我会得到
ValueError: could not convert string to float: $5.41所以我该怎么做?
I have a column named 'market_cap_(in_us_$)' which values are like:
$5.41 $18,160.50 $9,038.20 $8,614.30 $368.50 $2,603.80 $6,701.50 $8,942.40My final goal is to be able to filter based on specific numeric values (for example, > 2000.00).
By reading other questions in this site, I followed the instructions as:
cleaned_data['market_cap_(in_us_$)'].replace( '$', '', regex = True ).astype(float)However, I receiving the following error
TypeError: replace() got an unexpected keyword argument 'regex'If I remove the "regex = True" from the replace arguments, I get
ValueError: could not convert string to float: $5.41So, what should I do?
最满意答案
这里给出了正确使用的正则表达式,因为您想要移除$和:
In [7]: df['market_cap_(in_us_$)'].replace('[\$,]', '', regex=True).astype(float) Out[7]: 0 5.41 1 18160.50 2 9038.20 3 8614.30 4 368.50 5 2603.80 6 6701.50 7 8942.40 Name: market_cap_(in_us_$), dtype: float64但既然你得到了keyword argument 'regex'错误,你必须使用一个非常旧的版本,并且应该更新。
The right regular expression to use is given here, as you want to remove the $ and ,:
In [7]: df['market_cap_(in_us_$)'].replace('[\$,]', '', regex=True).astype(float) Out[7]: 0 5.41 1 18160.50 2 9038.20 3 8614.30 4 368.50 5 2603.80 6 6701.50 7 8942.40 Name: market_cap_(in_us_$), dtype: float64But since you got that keyword argument 'regex' error, you must be using a very old version, and should update.
更多推荐
发布评论