使用set
,用groupby.agg
+issubset
聚合,用isin
+boolean indexing过滤:
Type = {3,4,5}
df['Date'] = pd.to_datetime(df['Date'])
keep = df.groupby('Date')['Type'].agg(Type.issubset)
out = df[df['Date'].isin(keep.index[keep])]
groupby.transform
的变种:
Type = {3,4,5}
df['Date'] = pd.to_datetime(df['Date'])
out = df[df.groupby('Date')['Type'].transform(Type.issubset)]
输出:
Date Type Value
2 2024-03-12 3 3
3 2024-03-12 4 5
4 2024-03-12 5 5
5 2024-03-13 3 3
6 2024-03-13 4 5
7 2024-03-13 5 2
中间体:
# keep
Date
2024-03-11 False
2024-03-12 True
2024-03-13 True
2024-03-14 False
Name: Type, dtype: bool
# df['Date'].isin(keep.index[keep])
# or
# df.groupby('Date')['Type'].transform(Type.issubset)
0 False
1 False
2 True
3 True
4 True
5 True
6 True
7 True
8 False
Name: Type, dtype: bool