'mode values under a categorical column in python appear in form of a list
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?
Solution 1:[1]
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']"]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |