Python Pandas DataFrame 大分数值的舍入 [英] Python Pandas DataFrame Rounding of big fraction values
问题描述
如何舍入大熊猫数据帧的大分数值.我想舍入给定数据帧图像的男性和女性得分之间的差距"列.我试过以下:
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()
但它不起作用?
推荐答案
考虑数据框的一些初始行,
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
现在,如果您想将 round
分数转换为 int
,请执行此操作,
Now, if you want to round
score to int
, do this,
df['Score'] = df['Score'].astype(int)
df
输出:
Year Score
0 1990 1
1 1992 2
2 2000 2
3 2003 2
而且,如果您想四舍五入到一些十进制数字,请说最多 2 位数字.注意:您可以通过将所需的值传递给 round()
来四舍五入为任意数量的数字.
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
输出:
Year Score
0 1990 1.00
1 1992 2.38
2 2000 2.51
3 2003 2.96
如果要按ceil
或floor
舍入,请使用np.ceil(series)
或np.floor(系列)
分别.
If you want to round by ceil
or by floor
, then use np.ceil(series)
or np.floor(series)
respectively.
这篇关于Python Pandas DataFrame 大分数值的舍入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!