gpt4 book ai didi

python - 将对象转换为在 pandas 中 float 并替换 $ 符号

转载 作者:太空宇宙 更新时间:2023-11-04 00:45:36 25 4
gpt4 key购买 nike

我对 Pandas 还很陌生,我正在做一个项目,我有一个如下所示的列:

AverageTotalPayments
$7064.38
$7455.75
$6921.90
ETC

我正在尝试从中获取成本因素,其中成本可能高于 7000。首先,此列是一个对象。因此,我知道我可能无法将它与数字进行比较。我的代码如下所示:

import pandas as pd 
health_data = pd.read_csv("inpatientCharges.csv")

state = input("What is your state: ")
issue = input("What is your issue: ")
#This line of code will create a new dataframe based on the two letter state code
state_data = health_data[(health_data.ProviderState == state)]
#With the new data set I search it for the injury the person has.
issue_data=state_data[state_data.DRGDefinition.str.contains(issue.upper())]
#I then make it replace the $ sign with a '' so I have a number. I also believe at this point my code may be starting to break down.
issue_data = issue_data['AverageTotalPayments'].str.replace('$', '')
#Since the previous line took out the $ I convert it from an object to a float
issue_data = issue_data[['AverageTotalPayments']].astype(float)
#I attempt to print out the values.
cost = issue_data[(issue_data.AverageTotalPayments >= 10000)]
print(cost)

当我运行这段代码时,我只是得到了 nan。不完全是我想要的。任何错误的帮助都会很棒!提前谢谢你。

最佳答案

试试这个:

In [83]: df
Out[83]:
AverageTotalPayments
0 $7064.38
1 $7455.75
2 $6921.90
3 aaa

In [84]: df.AverageTotalPayments.str.extract(r'.*?(\d+\.*\d*)', expand=False).astype(float) > 7000
Out[84]:
0 True
1 True
2 False
3 False
Name: AverageTotalPayments, dtype: bool

In [85]: df[df.AverageTotalPayments.str.extract(r'.*?(\d+\.*\d*)', expand=False).astype(float) > 7000]
Out[85]:
AverageTotalPayments
0 $7064.38
1 $7455.75

关于python - 将对象转换为在 pandas 中 float 并替换 $ 符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39799859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com