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
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
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.