PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 11, 2022

[FIXED] How to format special decimals in pandas DataFrame

 August 11, 2022     dataframe, decimal, pandas, python     No comments   

Issue

In a pandas DataFrame column I have numbers like this

2840.189941

rounded to two decimals:

sp.loc['2019-05-13', 'Open'].round(2) 

​instead of getting 2840.19, I would like to format the numbers to .25 increments, so that I get this format:

2840.25
2840.50
2840.75
...

Any Ideas how to accomplish this?


Solution

I think you need multiple by 4, use Series.round and divide by 4:

df = pd.DataFrame({'Open': range(284018, 284100, 5)}) / 100

df['new'] = df['Open'].mul(4).round().div(4)
print (df)
       Open      new
0   2840.18  2840.25
1   2840.23  2840.25
2   2840.28  2840.25
3   2840.33  2840.25
4   2840.38  2840.50
5   2840.43  2840.50
6   2840.48  2840.50
7   2840.53  2840.50
8   2840.58  2840.50
9   2840.63  2840.75
10  2840.68  2840.75
11  2840.73  2840.75
12  2840.78  2840.75
13  2840.83  2840.75
14  2840.88  2841.00
15  2840.93  2841.00
16  2840.98  2841.00


Answered By - jezrael
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing