pandas :按A列对数据进行分组,按B列的现有值过滤A [英] Pandas: Group Data by column A, Filter A by existing values of column B

查看:44
本文介绍了 pandas :按A列对数据进行分组,按B列的现有值过滤A的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是熊猫的新手,想创建一个包含分组和过滤后数据的新数据集.现在,我的数据集包含两列如下所示(第一列包含A,B或C,第二列包含值):

I'm new to pandas and want to create a new dataset with grouped and filtered data. Right now, my dataset contains two columns looking like this (first column with A, B or C, second with value):

A 1 

A 2

A 3

A 4 

B 1

B 2

B 3 

C 4

->现在,我想按第一列的键(A,B,C)进行分组,并仅显示存在值1和2的键.这样我的新数据集看起来就像:

--> now I want to group by the keys of the first column (A,B,C) , and show only the keys, where the values 1 AND 2 exist. So that my new data set looks like:

A 1

A 2

B 1

B 2

直到现在,我只能打印所有内容,但不知道如何过滤:

Until now, I'm only able to print everything but I don't know how to filter:

for name, group in data.groupby('keys'):
   print(name)
   print(group)

感谢您的帮助!

推荐答案

您可以使用:

df = df.loc[(df['col2'] == 1) | (df['col2'] == 2)]

然后过滤不包含两个值的组:

And then filter the groups that dont contains both values:

df = df.groupby('col1').filter(lambda x: any(x['col2'] == 2))
df = df.groupby('col1').filter(lambda x: any(x['col2'] == 1))

示例:

  col1  col2
0    A     1
1    A     2
2    A     3
3    A     4
4    B     1
5    B     2
6    B     3
7    C     4
8    C     1

输出:

  col1  col2
0    A     1
1    A     2
4    B     1
5    B     2

这篇关于 pandas :按A列对数据进行分组,按B列的现有值过滤A的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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