Pivot table error:1 ndim Categorical are not supported at this time

Pivot table error:1 ndim Categorical are not supported at this time

我的目标是通过 'label' 绘制 'score',我不关心 "date" 和 "Cusip"。我想使用 'pivot' 重塑数据,以便每个标签都在一列中,我可以绘制它的箱线图。

              date   Cusip    Label Score
663182  2015-07-31  00846UAG    AAA 138.15
663183  2015-07-31  00846UAH    AAA 171.93
663184  2015-07-31  00846UAJ    AAA 175.67
663185  2015-07-31  023767AA    BB  187.92
663186  2015-07-31  023770AA    BB  176.25

t.pivot(index=['date','Cusip'],columns='Label',values='Score')

错误显示:

NotImplementedError: > 1 ndim Categorical are not supported at this time

更多详情:

C:\Anaconda3\lib\site-packages\pandas\core\categorical.py in __init__(self, values, categories, ordered, name, fastpath, levels)
    285             try:
--> 286                 codes, categories = factorize(values, sort=True)
    287             except TypeError:

C:\Anaconda3\lib\site-packages\pandas\core\algorithms.py in factorize(values, sort, order, na_sentinel, size_hint)
    184     uniques = vec_klass()
--> 185     labels = table.get_labels(vals, uniques, 0, na_sentinel, True)
    186 

pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_labels (pandas\hashtable.c:13921)()

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

您确实应该使用 pivot_table,因为您的 date 列中有重复条目。

pd.pivot_table(df, values='Score', index=['date', 'Cusip'], columns=['Label']).boxplot()

作为 .pivot_table() 的替代方法,它可能会执行不需要的聚合,您可以执行

df.set_index(['date', 'Cusip','Label'])['Score'].unstack()