Skip to main content
 首页 » 编程设计

python之如何消除python中的分组对象

2025年05月04日13rubylouvre

假设我有如下数据框

customer    day   
1       2016/12/21 
1       2016/12/30 
1       2017/1/2 
2       2016/12/4 
2       2016/12/10 
3       2017/1/3 
3       2017/1/5 

我想剔除2017/1/1之后访问的客户

我想要的结果是

customer   day 
2        2016/12/4 
2        2016/12/10 

我尝试与客户分组。

df.groupby(df.customer) 

或

df[df.day.dt.year<=2017] 

但我不知道如何消除。 我想通过客户需要迭代。

告诉我如何在特定条件下淘汰客户。

请您参考如下方法:

非 groupby 更快的解决方案:

df = df[~df['customer'].isin(df.loc[df['day'] > '2017-01-01', 'customer'])] 
print (df) 
   customer        day 
3         2 2016-12-04 
4         2 2016-12-10 

详细信息:

print (df.loc[df['day'] > '2017-01-01', 'customer']) 
2    1 
5    3 
6    3 
Name: customer, dtype: int64 

谢谢,Zero,想法:

df = df[~df['customer'].isin(df.loc[df['day'] > '2017-01-01', 'customer'].unique())] 

时间:

np.random.seed(123) 
N = 10000 
L = pd.date_range('2016-01-01', periods=400) 
df = pd.DataFrame({'day': np.random.choice(L, N), 
                   'customer':np.random.randint(1000, size=N)}) 
df = df.sort_values([ 'customer','day']) 
print (df) 
 
In [357]: %timeit df[~df['customer'].isin(df.loc[df['day'] > '2017-01-01', 'customer'])] 
1000 loops, best of 3: 932 µs per loop 
 
In [358]: %timeit df[~df['customer'].isin(df.loc[df['day'] > '2017-01-01', 'customer'].unique())] 
1000 loops, best of 3: 987 µs per loop 

In [359]: %timeit df.groupby('customer').filter(lambda x: ~(x.day>'2017/1/1').any()) 
1 loop, best of 3: 397 ms per loop 
 
In [360]: %timeit df.groupby('customer').filter(lambda x: x.day.le('2017/1/1').all()) 
1 loop, best of 3: 432 ms per loop 

In [361]: %timeit df.groupby('customer').filter(lambda x : (x.day<'2017-01-01').all()) 
1 loop, best of 3: 394 ms per loop 
 
In [362]: %timeit df.loc[~df.customer.isin(df.loc[df.day>'2017-01-01',].customer),:] 
1000 loops, best of 3: 1.25 ms per loop 
 
def wen(df): 
    df1 = df.drop_duplicates(['customer'],keep='last') 
 
    return df.loc[df.customer.isin(df1.loc[df1.day<'2017-01-01','customer']),:] 
 
 
In [363]: %timeit (wen(df)) 
1000 loops, best of 3: 1.81 ms per loop