Python Pandas DataFrame Rounding of big fraction values [closed]

不羁的心 提交于 2019-12-13 11:11:28

问题


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


回答1:


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://stackoverflow.com/questions/54022244/python-pandas-dataframe-rounding-of-big-fraction-values

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