Could you help me with this problem. x is already an integer. But I am getting this problem , If I use 90 instead of x , code runs but with x variable doesn't work.
split_ratio=[3,1,1]
x=split_ratio[0]/sum(split_ratio)*data.shape[0]
print(x)
print(data[0:x,:])
Output ;
90.0
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-38-0e56a1aca0a0> in <module>()
2 x=split_ratio[0]/sum(split_ratio)*data.shape[0]
3 print(x)
----> 4 print(data[0:x,:])
TypeError: slice indices must be integers or None or have an __index__ method
Whenever you do division by /
, it always returns a float and not an integer, although the answer may be an integer (with nothing after decimal points).
To fix this, there are two ways, one is to use the int()
function and the other is to use floor division //
.
So, you can either do
x=int(split_ratio[0]/sum(split_ratio)*data.shape[0])
or
x=split_ratio[0]//sum(split_ratio)*data.shape[0]
Now, when you will do print(x)
the output will be 90
and not 90.0
as 90.0
meant that it was a float and 90
means that now it is an integer.