过滤多列 pandas [英] Filtering multiple columns Pandas

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

问题描述

我有一种方法,它将熊猫数据框作为输入:

I have a method which takes a pandas dataframe as an input:

def dfColumnFilter(df, columnFilter, columnName):
    ''' Returns a filtered DataFrame

    Keyword arguments: 
    df           :  DataFrame in which to apply the filter
    columnFilter :  The list of which to filter by
    columnName   :  The DataFrame column to apply the columnFilter to '''

    for column_filter in columnFilter:
        df=df[df[columnName] == column_filter]
        return df

问题是如何使n列适合这项工作?

推荐答案

您可以使用 *args 关键字以传递对列表:

You can use the *args keyword to pass a list of pairs:

def filter_df(df, *args):
    for k, v in args:
        df = df[df[k] == v]
    return df

它可以这样使用:

df = pd.DataFrame({'a': [1, 2, 1, 1], 'b': [1, 3, 3, 3]})

>>> filter_df(df, ('a', 1), ('b', 2))
    a   b
2   1   3
3   1   3

注意

从理论上讲,您可以使用**kwargs,它会更令人愉悦:

In theory, you could use **kwargs, which would have a more pleasing usage:

filter_df(df, a=1, b=2)

但是您只能将其用于名称为有效Python标识符的列.

but then you could only use it for columns whose names are valid Python identifiers.

修改

有关更好的实施点,请参见@Goyo的评论.

See comment below by @Goyo for a better implementation point.

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

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