python - pd.cut:缓冲区的维数错误(预期为 1,得到 2)
问题描述
我有以下两段 Python 代码:
import pandas
ratio = [0.01, 0.2, 0.45, 0.7, 0.9, 1.01, 1.05, 1.07, 1.23, 1.78, 2.56, 3.12, 5.01, 6.21]
our_bins = [0, 0.2, 0.5, 0.8334, 1.199, 1.999, 4.999, 1000],
our_labels = ['Very Negative', 'Negative', 'Slightly Negative', 'Neutral',
'Slightly Positive', 'Positive', 'Very Positive']
pd.cut(ratio,
bins = our_bins,
right = False,
labels = our_labels)
import pandas
ratio = [0.01, 0.2, 0.45, 0.7, 0.9, 1.01, 1.05, 1.07, 1.23, 1.78, 2.56, 3.12, 5.01, 6.21]
pd.cut(ratio,
bins = [0, 0.2, 0.5, 0.8334, 1.199, 1.999, 4.999, 1000],
right = False,
labels = ['Very Negative', 'Negative', 'Slightly Negative', 'Neutral',
'Slightly Positive', 'Positive', 'Very Positive'])
为什么后者输出具有正确类别的数组而前者输出此错误?
ValueError: Buffer has wrong number of dimensions (expected 1, got 2)
解决方案
our_bins
不是列表,而是列表的元组,因为您在行尾添加了一个逗号
our_bins = [0, 0.2, 0.5, 0.8334, 1.199, 1.999, 4.999, 1000], # <- HERE
所以:
import pandas
ratio = [0.01, 0.2, 0.45, 0.7, 0.9, 1.01, 1.05, 1.07, 1.23, 1.78, 2.56, 3.12, 5.01, 6.21]
our_bins = [0, 0.2, 0.5, 0.8334, 1.199, 1.999, 4.999, 1000]
our_labels = ['Very Negative', 'Negative', 'Slightly Negative', 'Neutral',
'Slightly Positive', 'Positive', 'Very Positive']
pd.cut(ratio,
bins = our_bins,
right = False,
labels = our_labels)
输出:
['Very Negative', 'Negative', 'Negative', 'Slightly Negative', 'Neutral', ..., 'Slightly Positive', 'Positive', 'Positive', 'Very Positive', 'Very Positive']
Length: 14
Categories (7, object): ['Very Negative' < 'Negative' < 'Slightly Negative' < 'Neutral' <
'Slightly Positive' < 'Positive' < 'Very Positive']
推荐阅读
- scikit-learn - 如 scikit-learn 的文档中所述,如何在 cv 中使用 TimeSeriesSplit
- php - PHP Web 服务的 json 数组响应
- javascript - 在 HTMLDialogElement.showModal() 上添加 jquery-ui datepicker(绕过伪背景)
- javascript - 如何在复选框检查中仅显示特定的类数据
- cython - 包装基于宏的库时“错误:'do'之前的预期表达式”
- javascript - 优化函数以适用于多个名称元素
- excel - VBA - 将多个选定的文件从一个文件夹复制到另一个
- angular - 当我们需要在 Visual Studio 代码上创建新的 Angular 项目时,是否必须安装 node 和 npm?
- python - 数据集直接来自 tf.train.SequenceExample
- apache-spark - ai/h2o/extensions/stacktrace/StackTraceExtension:不支持的major.minor 52.0版