[英]convert pandas float series to int
I am discretizing my series for a learner.我正在为学习者离散我的系列。 I really need the series to be in float, and I really need to avoid for loops.
我真的需要这个系列处于浮动状态,我真的需要避免 for 循环。
How do I convert this series from float to int?如何将此系列从浮点数转换为整数?
Here is my function that is currently failing:这是我目前失败的功能:
def discretize_series(s,count,normalized=True):
def discretize(value,bucket_size):
return value % bucket_size
if normalized:
maximum = 1.0
else:
minimum = np.min(s)
s = s[:] - minimum
maximum = np.max(s)
bucket_size = maximum / float(count)
Here is the line that causes the function to fail:这是导致函数失败的行:
s = int((s[:] - s[:] % bucket_size)/bucket_size)
The int() induces a casting error: I am unable to cast the pandas series as an int series. int() 导致转换错误:我无法将 pandas 系列转换为 int 系列。
return s
If I remove the int(), the function works, so I may just see if I can get it to work anyway.如果我删除 int(),该函数就可以工作,所以我可能只是看看我是否可以让它工作。
The regular python int
function only works for scalars.常规 python
int
函数仅适用于标量。 You should either use a numpy function to round the data, either您应该使用 numpy 函数来舍入数据,或者
s = np.round((s - s % bucket_size) / bucket_size) #to round properly; or
s = np.fix((s - s % bucket_size) / bucket_size) #to round towards 0
and if you actually want to convert to an integer type, use如果您确实想转换为整数类型,请使用
s = s.astype(int)
to cast your array.投射你的数组。
y = [0.1234, 0.6789, 0.5678]
pandas
Seriespandas
系列s = pd.Series(data=y)
print(s.round(3))
returns返回
0 0.123
1 0.679
2 0.568
dtype: float64
print(s.astype(int))
returns返回
0 0
1 0
2 0
dtype: int64
pd.Series(data=y).round(3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.