我正在尝试在新变量中创建变量的四分位数组。我收到一条错误消息,但不确定为什么。
我写道:
bin_edges = ['0.000065', '0.207575','0.383831','0.713857','32.985763']
bin_names = ['low','mod_low','medium','high']
df['popularity_levels']= pd.cut(df['popularity'], bin_edges, labels=bin_names)
df.head()
然后:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-49-b6e8c834de1b> in <module>()
----> 1 df['popularity_levels']= pd.cut(df['popularity'], bin_edges, labels=bin_names)
2 df.head()
/opt/conda/lib/python3.6/site-packages/pandas/core/reshape/tile.py in cut(x, bins, right, labels, retbins, precision, include_lowest)
128 bins = np.asarray(bins)
129 bins = _convert_bin_to_numeric_type(bins, dtype)
--> 130 if (np.diff(bins) < 0).any():
131 raise ValueError('bins must increase monotonically.')
132
/opt/conda/lib/python3.6/site-packages/numpy/lib/function_base.py in diff(a, n, axis)
1766 return diff(a[slice1]-a[slice2], n-1, axis=axis)
1767 else:
-> 1768 return a[slice1]-a[slice2]
1769
1770
TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('<U9') dtype('<U9') dtype('<U9')
我遇到以下错误:
{{1}}
错误是什么意思?我认为这可能与将新变量的数据类型定义为float有关。那是正确的,我该如何解决?
答案 0 :(得分:2)
bin_edges
应该是浮点数:
bin_edges = ['0.000065', '0.207575','0.383831','0.713857','32.985763']
# should instead be
bin_edges = [0.000065, 0.207575, 0.383831, 0.713857, 32.985763]
发生错误,因为此列表已转换为numpy数组:
In [11]: np.array(['0.000065', '0.207575','0.383831','0.713857','32.985763'])
Out[11]:
array(['0.000065', '0.207575', '0.383831', '0.713857', '32.985763'],
dtype='<U9')
(这里dtype='<U9'
表示9个字符的unicode。)
In [12]: np.array(['0.000065', '0.207575','0.383831','0.713857','32.985763']) - 1
TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('<U9') dtype('<U9') dtype('<U9')