Working with pandas data frame, where one of the columns, say col1 has floating point values, I am trying to divide each of these values with a pre defined constant, say A, and then save the results as integer values.
A = 0.5
Following is the data in col1
df["col1"]
0 0.800000
1 0.883333
2 0.883333
3 1.000000
4 1.000000
5 1.300000
6 1.300000
7 1.500000
8 1.500000
9 2.000000
10 2.000000
11 2.500000
12 2.500000
After applying
df["new_col"] = (df["col1"] / A)
It gives
0 1.600000
1 1.766667
2 1.766667
3 2.000000
4 2.000000
5 2.600000
6 2.600000
7 3.000000
8 3.000000
9 4.000000
10 4.000000
11 5.000000
12 5.000000
which is fine, but as soon as I add .astype(int) to the above code, it can be observed at index 9 and 10 the values are 3 and 3 whereas it should be 4 and 4 respectively.
df["new_col"] = (df["col1"] / A).astype(int)
df["new_col"]
0 1
1 1
2 1
3 2
4 2
5 2
6 2
7 3
8 3
9 3
10 3
11 5
12 5
The other ways I am trying to obtain the result are
df["new_col"] = math.floor(df["col1"] / A )
and
df["new_col"] = int( df["col1"] / A)
Both of which gives me TypeError saying:
TypeError: cannot convert the series to class 'float' and TypeError: cannot convert the series to class 'int' respectively.
Please let me know how should I resolve the above issues.
round(0)maybe?