简体   繁体   English

pd.cut:缓冲区的维数错误(预期为 1,得到 2)

[英]pd.cut: Buffer has wrong number of dimensions (expected 1, got 2)

I have the following two pieces of Python code:我有以下两段 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'])

Why does the latter output an array with its correct categories whereas the former outputs this error?为什么后者输出具有正确类别的数组,而前者输出此错误?

ValueError: Buffer has wrong number of dimensions (expected 1, got 2)

our_bins is not a list but a tuple of list because you added a comma at the end of the line our_bins不是列表而是列表的元组,因为您在行尾添加了一个逗号

our_bins = [0, 0.2, 0.5, 0.8334, 1.199, 1.999, 4.999, 1000],  # <- HERE

So:所以:

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)

Output:输出:

['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']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 缓冲区的维数错误(预期为 1,得到 2) - Buffer has wrong number of dimensions (expected 1, got 2) pd.insert ValueError:缓冲区的维数错误(预期为 1,得到 2) - pd.insert ValueError: Buffer has wrong number of dimensions (expected 1, got 2) Pandas ValueError:缓冲区的维数错误(预期为 1,得到 2) - Pandas ValueError: Buffer has wrong number of dimensions (expected 1, got 2) 熊猫:“ ValueError:缓冲区的维数错误(预期1,得到0) - Pandas: "ValueError: Buffer has wrong number of dimensions (expected 1, got 0) ValueError:如果在语句中,则缓冲区的维数错误(预期为1,得到2) - ValueError: Buffer has wrong number of dimensions (expected 1, got 2) on if in statement Python scikits - 缓冲区的维数错误(预期1,得2) - Python scikits - Buffer has wrong number of dimensions (expected 1, got 2) Cython ValueError:缓冲区的维数错误(预期2,得3) - Cython ValueError: Buffer has wrong number of dimensions (expected 2, got 3) 缓冲区的维数错误(预期为 1,得到 2)。 如何拟合尺寸问题? - Buffer has wrong number of dimensions (expected 1, got 2). How to fit the dimensions problem? pandas:转换数据帧集合时,缓冲区的维数错误(预期为1,得0) - pandas: Buffer has wrong number of dimensions (expected 1, got 0) when transforming a dataframe column of sets ValueError:使用Seaborn时,缓冲区的维数错误(预期为1,为2) - ValueError: Buffer has wrong number of dimensions (expected 1, got 2) when using seaborn
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM