在Python Pandas中将带有$的货币转换为数字 [英] converting currency with $ to numbers in Python pandas

查看:941
本文介绍了在Python Pandas中将带有$的货币转换为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在pandas数据框中有以下数据:

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中将带有$的货币转换为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆