Используйте transform
с nunique
и сравните по 1
с eq
(==
), последний фильтр по boolean indexing
:
#if exist multiple types
#df = df[df['type'].isin(['C','P'])]
df = df[df.groupby(['code', 'date', 'strike'])['type'].transform('nunique').eq(1)]
print (df)
code date type strike settlement
id
1194926 CBT_21_G2012_S 2012-01-04 C 102.0 28.390625
1194905 CBT_21_G2012_S 2012-01-04 C 103.0 27.406250
1194908 CBT_21_G2012_S 2012-01-04 C 104.0 26.390625
1194989 CBT_21_G2012_S 2012-01-04 C 106.0 24.390625
Деталь
print (df.groupby(['code', 'date', 'strike'])['type'].transform('nunique'))
id
1195001 2
1195093 2
1194926 1
1194944 2
1195109 2
1194905 1
1195008 2
1195123 2
1194908 1
1194980 2
1195025 2
1194981 2
1195063 2
1194960 2
1195102 2
1194989 1
Name: type, dtype: int64
РЕДАКТИРОВАТЬ: Для значений подкачки используйте map
по словарю:
df['type'] = df['type'].map({'C':'P', 'P':'C'})
print (df)
code date type strike settlement
id
1194926 CBT_21_G2012_S 2012-01-04 P 102.0 28.390625
1194905 CBT_21_G2012_S 2012-01-04 P 103.0 27.406250
1194908 CBT_21_G2012_S 2012-01-04 P 104.0 26.390625
1194989 CBT_21_G2012_S 2012-01-04 P 106.0 24.390625