在尝试使用sort_values()方法对熊猫数据框进行排序时,出现意外的KeyError

时间:2019-01-20 10:21:58

标签: python-3.x pandas sorting

在尝试使用sort_values()方法对熊猫数据框进行排序时,出现意外的KeyError。我对此感到困惑。这是我的玩具数据和代码:

toy.to_json()
'{"count":{"P1550":46,"P1943":311,"P1974":353,"P2123":1,"P2305":451,"P2368":2,"P2378":468,"P2396":412,"P2403":1,"P2413":367},"mean":{"P1550":3.0,"P1943":24.0,"P1974":404.0,"P2123":27.0,"P2305":24.0,"P2368":3.0,"P2378":404.0,"P2396":13.0,"P2403":2.0,"P2413":2.0}}'

toy
         count  mean
promo_id        
P1550   46      3.0
P1943   311     24.0
P1974   353     404.0
P2123   1       27.0
P2305   451     24.0
P2368   2       3.0
P2378   468     404.0
P2396   412     13.0
P2403   1       2.0
P2413   367     2.0

toy.sort_values(by = ['mean'], axis = 1)

KeyError: 'mean'

0 个答案:

没有答案