I have a dataframe column like below
df2['over_ball']
0 6.0
1 5.0
2 4.0
3 4.0
4 3.0
5 2.0
6 1.0
7 6.0
8 5.0
9 4.0
10 3.0
11 3.0
12 2.0
13 1.0
So I thought of removing the digits after decimal and used astype(int)
. But I see that astype(int)
converts the values of 4.0 to 3 and 2.0 to 1.
df2['over_ball'].astype(int)
0 6
1 5
2 3
3 3
4 3
5 1
6 1
7 6
8 5
9 3
10 3
11 3
12 1
13 1
Why is this happening?
CodePudding user response:
Based on your comment, your error is reproducible.
df2 = pd.DataFrame({'over_ball': [3.99999999999, 1.99999999999]})
print(df2)
# Output:
over_ball
0 4.0 # instead of 3.99999999999
1 2.0 # instead of 1.99999999999
A simple cast doesn't work as expected:
>>> df2['over_ball'].astype(int)
0 3
1 1
Name: over_ball, dtype: int64
You need to use round
before astype(int)
:
>>> df2['over_ball'].round().astype(int)
0 4
1 2
Name: over_ball, dtype: int64
Tip:
pd.options.display.float_format = "{:,.20f}".format
print(df2)
# Output:
over_ball
0 3.99999999998999999917
1 1.99999999998999999917