Другой подход может быть:
df.loc[~df['Class'].isin(['Individual', 'Group']), 'Class'] = 'Other'
Вам нужно:
#convert column to numeric
df['Price'] = df['Price'].str.replace(',','.').astype(float)
#get top2 values from index
print (df['Price'].value_counts().iloc[:2])
78.0 3
199.0 3
Name: Price, dtype: int64
#filter rows with top2 values (78, 199)
df = df[df['Price'].isin(df['Price'].value_counts().iloc[:2].index)]
print (df)
InvoiceId StockCode Price
0 XXX ProductA 199.0
1 XXX ProductB 78.0
3 YYY ProductB 78.0
4 YYY ProductA 199.0
5 ZZZ ProductA 199.0
6 ZZZ ProductB 78.0
#count top2
df1 = pd.crosstab(df['InvoiceId'],
df['StockCode'])
print (df1)
StockCode ProductA ProductB
InvoiceId
XXX 1 1
YYY 1 1
ZZZ 1 1