I have following dataframe in pandas
ID Quantity Rate Product
1 10 70 MS
2 10 70 MS
3 100 70 MS
4 10 100 MS
5 700 65 HS
6 1100 65 HS
7 700 100 HS
I want to cap values with mean values in Quantity and Rate
For MS
if Quantity is greater than 100 and Rate is greater than 99
then it should be replaced by mean and For HS
if Quantity is greater than 1000 and Rate is greater than 99
then it should be replaced by mean.
I am using following way
mean_MS = df['Quantity'][(df['Product'] == 'MS') and (df['Quantity'] < 100)].mean()
But it does not work.
My desired dataframe would be
ID Quantity Rate Product
1 10 70 MS
2 10 70 MS
3 10 70 MS
4 10 70 MS
5 700 65 HS
6 700 65 HS
7 700 65 HS