I have a pandas dataframe df:
df =
category str_column
cat1 str1
cat2 str2
... ...
and a
parent_df =
category parent_category
cat1 parent_of_cat1
cat2 parent_of_cat2
and a list
faulty_categories =
['cat1', ...]
I need to get all the categories in df which are present in faulty_categories and replace them by their parent:
So, expected output:
df =
category str_column
parent_of_cat1 str1
cat2 str2
How to achieve this?
I tried:
df.loc[df['category'].isin(faulty_categories), 'category'] = parents_df.loc[parents_df['category'].isin(faulty_categories), 'parent']
But, this replaced category by NaN.