简体   繁体   English

按字典键值对过滤 pandas dataframe

[英]Filter pandas dataframe by dictionary key value pairs

Is there a more elegant way to filtering a dataframe by one column and then for each subset, further filtering by another column?有没有更优雅的方法来过滤 dataframe 一个列,然后为每个子集,进一步过滤另一列? And have the resulting data in one dataframe?并将结果数据保存在一个 dataframe 中吗? The filtering information is in a dictionary.过滤信息在字典中。 The first filter is on col1 using the dict key.第一个过滤器位于col1上,使用 dict 键。 The 2nd filter is on col3 using its corresponding value.第二个过滤器在col3上使用其相应的值。

df = pd.DataFrame({'col1': [1,1,1,2,2], 'col2': [2,2,2,2,2], 'col3': [1,6,7,5,9]})

df looks like the following df如下所示

    |col1|col2|col3|
    |1   |2   |1   |
    |1   |2   |6   |
    |1   |2   |7   |
    |2   |2   |5   |
    |2   |2   |9   |

filter_dict = {1:5, 2:7}

df_new = df.somefunction(filter_dict)

Where col1 is 1, filter where col3 value is greater than 5. Where col1 is 2, filter by col3 value is greater than 7. This would result:col1为 1 时,过滤col3值大于 5 的位置。当col1为 2 时,按col3值过滤大于 7。这将导致:

df_new

    |col1|col2|col3|
    |1   |2   |6   |
    |1   |2   |7   |
    |2   |2   |9   |

List comprehension and boolean indexing with concat列表理解和 boolean 索引与 concat

df_new = pd.concat([df[(df['col1'] == k) & (df['col3'] > v)] for k,v in filter_dict.items()])

   col1  col2  col3
1     1     2     6
2     1     2     7
4     2     2     9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM