我有以下数据框:
dct ={'store':('A','A','A','A','A','B','B','B','C','C','C'),
'station':('aisle','aisle','aisle','window','window','aisle','aisle','aisle','aisle','window','window'),
'produce':('apple','apple','orange','orange','orange','apple','apple','orange','apple','apple','orange')}
df = pd.DataFrame(dct)
print(df)
store station produce
A aisle apple
A aisle apple
A aisle orange
A window orange
A window orange
B aisle apple
B aisle apple
B aisle orange
C aisle apple
C window apple
C window orange
基于以下条件筛选df:[基于商店、站点和产品的重复数据数量]与[基于商店、站点和产品的总计数]不同。
换句话说,如果某个商店只有基于商店、站点和产品的重复行,则删除该商店,但是如果存在一个非重复记录,则包括其余行。
预期的数据框遍历。
store station produce
A aisle apple
A aisle apple
A aisle orange
A window orange ->exclude because store, station and produce match
A window orange ->exclude because store, station and produce match
B aisle apple
B aisle apple
B aisle orange
C aisle apple
C window apple
C window orange
期望的数据框:
store station produce
A aisle apple
A aisle apple
A aisle orange
B aisle apple
B aisle apple
B aisle orange
C aisle apple
C window apple
C window orange
来自商店'B'的苹果被包含在内,因为同一商店-站点中也存在 '橙子',这使得它成为一个例外。
从概念上讲,我理解该如何做,但无法将其翻译成代码。
s = (df.duplicated(subset = ['store','station','produce'], keep=False))
sample = df[df.groupby(['store','station'])['station_ID'].sum().eq(dupli_count)] --> something going wrong here