I am using this code to get the mode of a categorical column:
df.groupby('user_id')['product'].agg(pd.Series.mode).reset_index().rename(columns = {'product': 'most_used_product'}).astype(str)
after running the above code, values under this column appear in a form of list per user like:
['hat' 'shirt' 'shoes']
['hat' 'shoes']
['shirt']
when I try to select the value of column using:
df[df['most_used_product']== "['hat' 'shirt' 'shoes']"
I get SyntaxError: invalid syntax. How can I select the value of column?
CodePudding user response:
You hav two errors, first the ending square bracket, second your data type is not string, use:
df[df['most_used_product'].astype(str)== "['hat', 'shirt', 'shoes']"]