简体   繁体   English

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

[英]How do I re-add decimal points to my Pandas column "Price" after converting it into an int?

I used the code:我使用了代码:

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

to turn my dataframe column from an object to an int so I can plot it.将我的dataframe列从 object 转换为int ,这样我就可以 plot 了。 Doing so has removed all symbols and turned them into empty strings including the decimal and now my prices has gone from $22,000.00 to 2200000 ($2,200,000).这样做删除了所有符号并将它们变成空字符串,包括小数点,现在我的价格从 $22,000.00 变为 2200000 ($2,200,000)。

How do I place the decimals back where they belong and keep my column as an int if possible?如何将小数放回它们所属的位置,并尽可能将我的列保留为int

I've tried decimals = pd.Series([2], index=['Price']) car_sales.round(decimals)我试过 decimals = pd.Series([2], index=['Price']) car_sales.round(decimals)

and nothing changed and other minor tweaks with no success.没有任何改变,其他小调整也没有成功。

Have you tried using something like this:您是否尝试过使用这样的东西:


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

By not including the .通过不包括. symbol inside your replace, you don't need to rearranje the entire series to account for the removed decimals.替换中的符号,您不需要重新排列整个系列来说明删除的小数点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM