Pandas DataFrame:如何在多个条件下选择行? [英] Pandas DataFrame : How to select rows on multiple conditions?

查看:633
本文介绍了Pandas DataFrame:如何在多个条件下选择行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据一系列需要满足的条件来选择DataFrame的行. 这些条件存储在字典中,格式为{column:max-value}.

I'm trying to select rows of a DataFrame based on a list of conditions that needs to be all satisfied. Those conditions are stored in a dictionary and are of the form {column: max-value}.

这是一个示例:dict = {'name': 4.0, 'sex': 0.0, 'city': 2, 'age': 3.0}

我需要选择相应属性小于或等于字典中相应值的所有DataFrame行.

I need to select all DataFrame rows where the corresponding attribute is less than or equal to the corresponding value in the dictionary.

我知道要基于两个或多个条件选择行,我可以写:

I know that for selecting rows based on two or more conditions I can write:

rows = df[(df[column1] <= dict[column1]) & (df[column2] <= dict[column2])]

我的问题是,如何以Python方式选择与字典中存在的条件匹配的行? 我尝试过这种方式,

My question is, how can I select rows that matches the conditions present in a dictionary in a Pythonic way? I tried this way,

keys = dict.keys() 
rows = df[(df[kk] <= dict[kk]) for kk in keys]

但是它给了我一个错误="[ expected",即使放置了[符号,该错误也不会消失.

but it gives me an error = "[ expected" that doesn't disappear even putting the [ symbol.

推荐答案

我们可以使用

we can use DataFrame.query() method like this:

In [109]: dct = {'name': 4.0, 'sex': 0.0, 'city': 2, 'age': 3.0}

In [110]: qry = ' and '.join(['{} <= {}'.format(k,v) for k,v in dct.items()])

In [111]: qry
Out[111]: 'name <= 4.0 and sex <= 0.0 and city <= 2 and age <= 3.0'

In [112]: df.query(qry)
...

这篇关于Pandas DataFrame:如何在多个条件下选择行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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