pandas :qcut错误:ValueError:容器边缘必须是唯一的: [英] pandas: qcut error: ValueError: Bin edges must be unique:
本文介绍了 pandas :qcut错误:ValueError:容器边缘必须是唯一的:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用pandas qcut方法计算两列的百分位数,如下所示:
I am trying to compute percentile of two columns using the pandas qcut method like below:
my_df['float_col_quantile'] = pd.qcut(my_df['float_col'], 100, labels=False)
my_df['int_col_quantile'] = pd.qcut(my_df['int_col'].astype(float), 100, labels=False)
列float_col_quantile
可以正常工作,但是列int_col_quantile
具有以下错误.知道我在这里做错了什么吗?我该如何解决这个问题?谢谢!
The column float_col_quantile
works fine, but the column int_col_quantile
has the following error. Any idea what I did wrong here? And how can I fix this problem? Thanks!
ValueError Traceback (most recent call last)
<ipython-input-19-b955e0b00953> in <module>()
1 my_df['float_col_quantile'] = pd.qcut(my_df['float_col'], 100, labels=False)
----> 2 my_df['int_col_quantile'] = pd.qcut(my_df['int_col'].astype(float), 100, labels=False)
/usr/local/lib/python3.4/dist-packages/pandas/tools/tile.py in qcut(x, q, labels, retbins, precision)
173 bins = algos.quantile(x, quantiles)
174 return _bins_to_cuts(x, bins, labels=labels, retbins=retbins,
--> 175 precision=precision, include_lowest=True)
176
177
/usr/local/lib/python3.4/dist-packages/pandas/tools/tile.py in _bins_to_cuts(x, bins, right, labels, retbins, precision, name, include_lowest)
192
193 if len(algos.unique(bins)) < len(bins):
--> 194 raise ValueError('Bin edges must be unique: %s' % repr(bins))
195
196 if include_lowest:
ValueError: Bin edges must be unique: array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 2., 2., 2., 2., 2., 2., 2.,
2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.,
2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.,
2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.,
2., 2., 2., 4., 4., 4., 4., 4., 4., 4., 4.,
4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4.,
4., 4., 4., 4., 4., 4., 4., 4., 8., 8., 8.,
8., 10.])
推荐答案
问题是pandas.qcut选择bin,以便每个bin/分位数中的记录数相同,但相同的值不能落在多个bin中/分位数.
The problem is pandas.qcut chooses the bins so that you have the same number of records in each bin/quantile, but the same value cannot fall in multiple bins/quantiles.
此处是解决方案列表.
这篇关于 pandas :qcut错误:ValueError:容器边缘必须是唯一的:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文