如何在将Pandas列“Price”转换为int后重新添加小数点?

csga3l58  于 2023-02-02  发布在  其他
关注(0)|答案(1)|浏览(99)

我使用的代码:

car_sales["Price"]=car_sales["Price"].int.replace('[\$\,\.]', '').astype(int)

将我的dataframe列从对象转换为int,这样我就可以绘制它了。这样做删除了所有符号,并将它们转换为包括小数点在内的空字符串,现在我的价格从22,000.00美元变成了2200,000美元。
如何将小数放回原处,并在可能的情况下将列保持为int
我试过小数= pd.Series([2],index=['Price'])car_sales.round(小数)
什么都没改变,其他小的调整也没有成功。

vuktfyat

vuktfyat1#

你有没有试过用这样的东西:

car_sales["Price"].str.replace('[$,]', '').astype(float).round(2)
# 0    22000.0
# 1    21000.0
# 2    22500.0
# 3    22100.0
# 4     2000.1
# Name: Price, dtype: float64

通过在replace中不包含.符号,您不需要重新排列整个系列来考虑删除的小数。

相关问题