如何使用query()方法过滤多索引数据帧

2024-03-28 17:03:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用query方法过滤多索引数据帧,但失败了

对于可复制的示例:

enter image description here

toy.to_json()
'{"["absolute_error","mean"]":{"5.0":0.4201680672,"16.0":0.4411764706,"7.0":0.3711787571,"29.0":0.4914948454,"15.0":0.4527027027},"["absolute_error","median"]":{"5.0":0.4201680672,"16.0":0.4411764706,"7.0":0.3606557377,"29.0":0.4914948454,"15.0":0.4527027027},"["absolute_error","min"]":{"5.0":0.4201680672,"16.0":0.4411764706,"7.0":0.2941176471,"29.0":0.4329896907,"15.0":0.4527027027},"["absolute_error","max"]":{"5.0":0.4201680672,"16.0":0.4411764706,"7.0":0.4587628866,"29.0":0.55,"15.0":0.4527027027},"["absolute_error","std"]":{"5.0":null,"16.0":null,"7.0":0.0828255044,"29.0":0.0827387832,"15.0":null},"["absolute_error","count"]":{"5.0":1,"16.0":1,"7.0":3,"29.0":2,"15.0":1},"["squared_error","mean"]":{"5.0":0.6897959184,"16.0":0.7253521127,"7.0":0.755749764,"29.0":0.846516747,"15.0":0.8851351351},"["squared_error","median"]":{"5.0":0.6897959184,"16.0":0.7253521127,"7.0":0.6727272727,"29.0":0.846516747,"15.0":0.8851351351},"["squared_error","min"]":{"5.0":0.6897959184,"16.0":0.7253521127,"7.0":0.6710526316,"29.0":0.7487179487,"15.0":0.8851351351},"["squared_error","max"]":{"5.0":0.6897959184,"16.0":0.7253521127,"7.0":0.9234693878,"29.0":0.9443155452,"15.0":0.8851351351},"["squared_error","std"]":{"5.0":null,"16.0":null,"7.0":0.1452518683,"29.0":0.1383083869,"15.0":null},"["squared_error","count"]":{"5.0":1,"16.0":1,"7.0":3,"29.0":2,"15.0":1}}'

toy.query('(squared_error,count) >= 2') # Here I use a convention that works in sorting the multi-indexed dataframe, i.e. passing the labels of both levels as a tuple

但这会返回一个异常:

UndefinedVariableError: name 'squared_error' is not defined

Tags: the方法counterrorminmeanquerynull