Python Pandas DataFrame Rounding of big fraction values [closed]

2024/10/10 4:23:16

How to round off big fraction values of a pandas DataFrame.I want to round off the "Gaps between male and female score" column of the given Dataframe image. I have tried following:

df.astype('int').round()   

but it doesn't work?

Image

Answer

Considering some initial rows of your dataframe,

    Year    Score
0   1990    1.00378
1   1992    2.37824
2   2000    2.51302
3   2003    2.96111

Now, if you want to round score to int, do this,

df['Score'] = df['Score'].astype(int)
df

Output:

    Year    Score
0   1990    1
1   1992    2
2   2000    2
3   2003    2

And, if you want to round upto some decimal digits say upto 2-digits. Note: you can round upto as many digits as you want by passing required value to round().

df['Score'] = df['Score'].round(2)
df

Output:

  Year  Score
0   1990    1.00
1   1992    2.38
2   2000    2.51
3   2003    2.96

If you want to round by ceil or by floor, then use np.ceil(series) or np.floor(series) respectively.

https://en.xdnf.cn/q/118495.html

Related Q&A

Splitting Data in Python?

it does not work. I want to split data as in code in lines attribute. class movie_analyzer:def __init__(self,s):for c in punctuation:import removiefile = open(s, encoding = "latin-1")movielis…

Use local function variable inside loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 7 years ago.Improve…

Groupby Pandas , calculate multiple columns based on date difference

I have a pandas dataframe shown below: CID RefID Date Group MID 100 1 1/01/2021 A 100 2 3/01/2021 A 100 3 4/01/20…

EC2 .bashrc and .bash_profile re-setting

Reason Im asking: pycurl requires both libcurl-devel and openssl-devel. To install these, I have these two lines the my .bash_profile: sudo yum install libcurl-devel sudo yum install -y openssl-develPr…

How to load mutiple PPM files present in a folder as single Numpy ndarray?

The following Python code creates list of numpy array. I want to load by data sets as a numpy array that has dimension K x M x N x 3 , where K is the index of the image and M x N x 3 is the dimension …

Python: Understanding Threading Module

While learning Pythons threading module Ive run a simple test. Interesting that the threads are running sequentially and not parallel. Is it possible to modify this test code so a program executes the …

How the program has control with the break statement [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Python: how to plot data coming from different excel sheets in the same chart

I need to create an interactive chart on python taking data from different sheets of an Excel file. I tried to create a for loop to take data from all the sheets automatically, but I manage to graph on…

Dynamic html table with django [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 6…

Driving costs - functions in python- EOFerror in if __name__ == __main__:

I am stuck with this question: Write a function driving_cost() with input parameters miles_per_gallon, dollars_per_gallon, and miles_driven, that returns the dollar cost to drive those miles. All items…