熊猫数据透视表页边距返回TypeError

时间:2020-03-17 15:09:10

标签: python pandas

从此数据开始:

                SUBJECT_CD  REGULAR_QUANTITY_REQUESTED
BEDS_CD                                               
307500099999     REG_ALGCC                          26
307500099999     REG_ENGCC                          26
307500099999      REG_ESCI                           1
307500099999      REG_GHG2                          26
307500099999    REG_GLHIST                          26
307500099999      REG_LENV                          26
307500099999      REG_USHG                          26
307500099999    REG_USHGNF                          26
310200010999     REG_ALGCC                         160
310200010991     REG_ALGCC                         160
310200010119     REG_ALGCC                          80
310200010191     REG_ALGCC                         130
310200010919     REG_ALGCC                          95
310200010911      REG_LENV                          95
310200099999     REG_ALGCC                         150
310200099999      REG_LENV                         100
310299999999     REG_ALGCC                          60
310299999999    REG_GEOMCC                           5
310209999999     REG_ALGCC                         130
310209999999  REG_ALGTRICC                          90

运行数据透视表时,它给我一个错误:

TypeError: unhashable type: 'Int64Index'

REGULAR_QUANTITY_REQUESTED都是int64,所以我不确定为什么不能只在末尾添加求和列。

代码:

pivot = pd.pivot_table(df,
                           index=df.index,
                           columns=['SUBJECT_CD'],
                           values=['REGULAR_QUANTITY_REQUESTED'],
                           aggfunc=[np.sum],
                           fill_value=0,
                           margins=True)

此方法无需使用margins行。

1 个答案:

答案 0 :(得分:0)

不确定是否使用margins时会有所不同,但该错误表明该问题与使用索引有关。

所以reset_index在这里就足够了:

pivot = pd.pivot_table(df.reset_index(),
                       index=['BEDS_CD'],
                       columns=['SUBJECT_CD'],
                       values=['REGULAR_QUANTITY_REQUESTED'],
                       aggfunc=[np.sum],
                       fill_value=0,
                       margins=True)

它给出:

                                    sum                                                                                                  
             REGULAR_QUANTITY_REQUESTED                                                                                                  
SUBJECT_CD                    REG_ALGCC REG_ALGTRICC REG_ENGCC REG_ESCI REG_GEOMCC REG_GHG2 REG_GLHIST REG_LENV REG_USHG REG_USHGNF   All
BEDS_CD                                                                                                                                  
307500099999                         26            0        26        1          0       26         26       26       26         26   183
310200010119                         80            0         0        0          0        0          0        0        0          0    80
310200010191                        130            0         0        0          0        0          0        0        0          0   130
310200010911                          0            0         0        0          0        0          0       95        0          0    95
310200010919                         95            0         0        0          0        0          0        0        0          0    95
310200010991                        160            0         0        0          0        0          0        0        0          0   160
310200010999                        160            0         0        0          0        0          0        0        0          0   160
310200099999                        150            0         0        0          0        0          0      100        0          0   250
310209999999                        130           90         0        0          0        0          0        0        0          0   220
310299999999                         60            0         0        0          5        0          0        0        0          0    65
All                                 991           90        26        1          5       26         26      221       26         26  1438