Change int value to .00 format

瘦欲@ 提交于 2020-08-10 20:28:27

问题


I have a dataframe -

   year  month             type   amount
0  2019  9          Not Applicable    8000.00       
1  2019  10         Not Applicable    7500.00       
2  2019  11         Goods & Services  14000.35      
3  2019  11         Not Applicable    7500.00       
4  2019  12         Goods & Services  10499.00      
5  2019  12         Not Applicable    9801.00   

I have column amount fully round of but I want to convert another column month to this format like this -

   year  month             type   amount
0  2019  9.00          Not Applicable    8000.00       
1  2019  10.00         Not Applicable    7500.00       
2  2019  11.00         Goods & Services  14000.35      
3  2019  11.00         Not Applicable    7500.00       
4  2019  12.00         Goods & Services  10499.00      
5  2019  12.00         Not Applicable    9801.00   

How can I achieve this thing.


回答1:


df.month = df.month.astype(float)

or

df['month'] = df['month'].astype(float)



回答2:


To convert into float with 2 decimal places :

df['month'] = df['month'].astype('float').map('{:,.2f}'.format)
df['month']

Output :

0     9.00
1    10.00
2    11.00
3    11.00
4    12.00
5    12.00



回答3:


Terminology is key here. If you just want to change the "format" within your jupyter notebook -- which has no impact when sent to excel -- then @SurajSubramanian 's answer should be the accepted solution. If you simply want to change the column to float format, then @nav610 's answer is correct, but the title of your question is specifically "Change int value to .00 format"

So, I mentioned, terminology is key, because if you acutally want to change the underlying value to end with .00, then your only option is to convert it to a string like so:

df['month'] = df['month'].astype(str) + '.00'

        year    month   type                 amount
0       2019    9.00    Not Applicable       8000.00
1       2019    10.00   Not Applicable       7500.00
2       2019    11.00   Goods & Services     14000.35
3       2019    11.00   Not Applicable       7500.00
4       2019    12.00   Goods & Services     10499.00
5       2019    12.00   Not Applicable       9801.00

See my comments on some of the answers for more context, but the answer is that it really depends on your actual use case what the best solution is.



来源:https://stackoverflow.com/questions/62846784/change-int-value-to-00-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!