I have a dataset with thousands of rows which has a few outliers in the 'value' columns.
df_test = pd.DataFrame({
'product': ['Egg', 'Egg', 'Egg', 'Small Egg','Small Egg','Small Egg','Small Egg', 'Wheat','Wheat','Wheat','Wheat','Wheat','Rice','Rice','Rice','Garlic','Garlic','Garlic','Garlic','Garlic','Tomato','Tomato','Tomato', 'Ananas'],
'value': ['13','5','3','28','5','4','5','28','28','28','1','1.5','7','4','4.3','140','143','149','320','5','400','10','15', '8']
})
I know which data is incorrect from comments available in another dataset, this one is basically the list of products (unique) with a comment on the maximum value to remove:
df_test_comment = pd.DataFrame({
'product': ['Egg', 'Small Egg', 'Wheat', 'Rice', 'Garlic','Tomato', 'Ananas'],
'What to remove': ['1st max','1st and 2nd max','1st, 2nd, and 3rd max', '1st max', '1st, 2nd, 3rd and 4th max','1st and 2nd max', 'NaN']
})
Because I have only a limited number of different comments ('1st max', '1st and 2nd max', '1st, 2nd, and 3rd max', '1st, 2nd, 3rd and 4th max'), I was thinking of using a for loop to delete in df_test the max value of a product if the comment in df_test_comment is '1st max' ; max value and second max value when '1st and 2nd max' etc.
The ideal output with the sample example would be this:
df_result = pd.DataFrame({
'product': ['Egg','Egg','Small Egg','Small Egg','Wheat','Wheat','Rice','Rice','Garlic','Tomato', 'Ananas'],
'Value': ['5','3','4','5','1','1.5','4','4.3','5','10','8']
})
Any idea how to tackle this cleaning?