Lis*_*isa 5 python pivot pandas
我的目标是按“标签”绘制“分数”,我不关心“日期”和“Cusip”。我想使用“枢轴”来重塑数据,以便每个标签都在一列中,我可以对其进行箱线图。
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')
Run Code Online (Sandbox Code Playgroud)
错误显示:
NotImplementedError: > 1 ndim Categorical are not supported at this time
Run Code Online (Sandbox Code Playgroud)
更多细节:
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)
Run Code Online (Sandbox Code Playgroud)
您确实应该使用,pivot_table因为您的列中有重复的条目date。
pd.pivot_table(df, values='Score', index=['date', 'Cusip'], columns=['Label']).boxplot()
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
8501 次 |
| 最近记录: |