如何解析 DataFrame 列中的所有值? [英] How to parse all the values in a column of a DataFrame?
本文介绍了如何解析 DataFrame 列中的所有值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
DataFrame df 有一列名为 amount
DataFrame df has a column called amount
import pandas as pd
df = pd.DataFrame(['$3,000,000.00','$3,000.00', '$200.5', '$5.5'], columns = ['Amount'])
df:
ID | Amount
0 | $3,000,000.00
1 | $3,000.00
2 | $200.5
3 | $5.5
我想解析列金额中的所有值并将金额提取为数字并忽略小数点.最终结果是如下所示的 DataFrame:
I want to parse all the values in column amount and extract the amount as a number and ignore the decimal points. End result is DataFrame that looks like this:
ID | Amount
0 | 3000000
1 | 3000
2 | 200
3 | 5
我该怎么做?
推荐答案
您可以使用 str.replace
带有双重转换 astype
:
You can use str.replace
with double casting by astype
:
df['Amount'] = (df.Amount.str.replace(r'[\$,]', '').astype(float).astype(int))
print (df)
Amount
0 3000000
1 3000
2 200
3 5
这篇关于如何解析 DataFrame 列中的所有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文