问题描述
我在熊猫数据框中有以下数据:
I have the following data in pandas dataframe:
state 1st 2nd 3rd
0 California $11,593,820 $109,264,246 $8,496,273
1 New York $10,861,680 $45,336,041 $6,317,300
2 Florida $7,942,848 $69,369,589 $4,697,244
3 Texas $7,536,817 $61,830,712 $5,736,941
我想用三列(第一、第二、第三)执行一些简单的分析(例如,sum、groupby),但是这三列的数据类型是对象(或字符串).
I want to perform some simple analysis (e.g., sum, groupby) with three columns (1st, 2nd, 3rd), but the data type of those three columns is object (or string).
所以我使用了以下代码进行数据转换:
So I used the following code for data conversion:
data = data.convert_objects(convert_numeric=True)
但是,转换不起作用,也许是由于美元符号.有什么建议吗?
But, conversion does not work, perhaps, due to the dollar sign. Any suggestion?
推荐答案
@EdChum 的回答很聪明而且效果很好.但是既然有不止一种烤蛋糕的方法......为什么不使用正则表达式?例如:
@EdChum's answer is clever and works well. But since there's more than one way to bake a cake.... why not use regex? For example:
df[df.columns[1:]] = df[df.columns[1:]].replace('[$,]', '', regex=True).astype(float)
对我来说,这更具可读性.
To me, that is a little bit more readable.
这篇关于用 $ 将货币转换为 Python pandas 中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!