pandas 过滤器与定位方法 [英] Pandas filter vs. loc method

查看:55
本文介绍了 pandas 过滤器与定位方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道熊猫中.filter方法和.loc方法之间的根本区别吗?他们似乎做同样的事情.谢谢.

Does anyone know the fundamental difference between the .filter method and the .loc method in Pandas? They seem to do the same thing. Thanks.

推荐答案

.loc[]

.loc[] is a Purely label-location based indexer for selection by label. It fails when the selection isn't found, only accepts certain types of input and works on only one axis of your dataframe.

df.filter()返回子集的行或列数据框根据指定索引中的标签.您可以沿任一轴进行过滤,并且可以采用比loc更为高级的方式进行过滤.

df.filter() returns Subset rows or columns of dataframe according to labels in the specified index. You can filter along either axis, and you can filter in more advanced ways than with loc.

filter将返回与调用者相同类型的对象,而loc将返回由标签指定的值(如果调用者是DF,则返回Series,如果调用者是Series,则返回标量).

filter will return the same type of object as the caller, whereas loc will return the value specified by the label (so a Series if caller is a DF, a scalar if caller is a Series).

简而言之,.loc用于访问呼叫者中的特定项目,.filter()用于将过滤器应用于呼叫者并仅返回与该过滤器匹配的项目.

In short, .loc is for accessing a specific item within the caller, .filter() is for applying a filter to the caller and returning only items which match that filter.

这篇关于 pandas 过滤器与定位方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆