首页 > 解决方案 > Python .loc 返回一个 TypeError

问题描述

我是使用 .loc 的新手,但每次我尝试使用它时,它都会返回“TypeError: 'DataFrame' object is not callable”。

例如,我无法让这个简单的代码(用于 pokemon API)工作:

print(df.loc(df('Attack') > 175))


TypeError: 'DataFrame' object is not callable

请有人帮忙,它真的让我烦恼为什么它不起作用?!?

标签: pythonpandastypeerror.loc

解决方案


索引(iloc和访问 column Attack)应该用方括号完成,而不是圆括号(因为它们不是函数)

例如

 df.loc[df['Attack'] > 175]

使用df('Attack'),您正在尝试调用df对象,这当然不是函数,因此不可调用


推荐阅读