【问题标题】:pd.cut: Buffer has wrong number of dimensions (expected 1, got 2) [closed]pd.cut:缓冲区的维数错误(预期为 1,得到 2)[关闭]
【发布时间】:2021-10-18 21:01:32
【问题描述】:

我有以下两段 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)

【问题讨论】:

    标签: python pandas error-handling


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-28
      • 2022-08-07
      • 1970-01-01
      • 2012-04-12
      • 1970-01-01
      • 2015-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多