检查第二列的条件后如何删除某些值?

时间:2019-05-17 09:15:58

标签: python pandas

假设df如下:

Product    Time
   1         1
   1         2
   1         3
   1         4
   2         1
   2         2
   2         3
   2         4
   2         5
   2         6
   2         7
   3         1
   3         2
   3         3
   4         1
   4         2
   4         3

我只想保留Product大于3的Time,其余的丢弃。 在上面的示例中,我做了

df.groupby(['Product']).size()

我得到以下输出:

1    4
2    7
3    3
4    3

基于此,在我的主要df中,我只想保留产品1和2

预期输出:

     Product    Time
       1         1
       1         2
       1         3
       1         4
       2         1
       2         2
       2         3
       2         4
       2         5
       2         6
       2         7

3 个答案:

答案 0 :(得分:3)

分组后,请改为使用transform.size,检查大于(gt)3的对象,并使用结果对数据框执行boolean indexing

df[df.groupby('Product').Time.transform('size').gt(3)]

      Product  Time
0         1     1
1         1     2
2         1     3
3         1     4
4         2     1
5         2     2
6         2     3
7         2     4
8         2     5
9         2     6
10        2     7

答案 1 :(得分:3)

使用GroupBy.transform来返回Series,其大小与原始大小相同,因此可以通过boolean indexing进行过滤:

df = df[df.groupby(['Product'])['Product'].transform('size') > 3]
print (df)
    Product  Time
0         1     1
1         1     2
2         1     3
3         1     4
4         2     1
5         2     2
6         2     3
7         2     4
8         2     5
9         2     6
10        2     7

详细信息

b = df.groupby(['Product'])['Product'].transform('size') > 3
a = df.groupby(['Product'])['Product'].transform('size')

print (df.assign(size=a, filter=b))
    Product  Time  size  filter
0         1     1     4    True
1         1     2     4    True
2         1     3     4    True
3         1     4     4    True
4         2     1     7    True
5         2     2     7    True
6         2     3     7    True
7         2     4     7    True
8         2     5     7    True
9         2     6     7    True
10        2     7     7    True
11        3     1     3   False
12        3     2     3   False
13        3     3     3   False
14        4     1     3   False
15        4     2     3   False
16        4     3     3   False

如果DataFrame不大,则可以使用DataFrameGroupBy.filter替代:

df = df.groupby(['Product']).filter(lambda x: len(x) > 3)

答案 2 :(得分:1)

如果您不打算使用assign操作并且想使用boolean indexing,则可以执行此操作。

g = df.groupby('Product')
t = g.transform('count')
df['c']=t #new column holding the count
df2=df[df['c'] > 3]
print(df2)

    Product  Time
0         1     1
1         1     2
2         1     3
3         1     4
4         2     1
5         2     2
6         2     3
7         2     4
8         2     5
9         2     6
10        2     7
11        3     1
12        3     2
13        3     3
14        4     1
15        4     2
16        4     3
    Product  Time  c
0         1     1  4
1         1     2  4
2         1     3  4
3         1     4  4
4         2     1  7
5         2     2  7
6         2     3  7
7         2     4  7
8         2     5  7
9         2     6  7
10        2     7  7