I have a problem with a column of my dataframe but I don't understand why there are trouble on my column cat.
Asked
Active
Viewed 1.8k times
-7
![](../../users/profiles/7992925.webp)
Corentin Moreau
- 99
- 1
- 1
- 10
-
2`df_cat_tot['cat'].unique()` You are attempting to use `.cat` which is a reserved method for pandas. You must use `['cat']` to access that column. – piRSquared May 21 '18 at 14:08
-
1In the future, please post your dataframe as text, not as an image, same goes for output/errors. – user3483203 May 21 '18 at 14:10
-
Thanks for your answer, i try with df_cat_tot['cat'].unique() but I ve the same error – Corentin Moreau May 21 '18 at 14:10
-
1Can you please post the output of `{type(i) for i in df_cat_tot['cat'].values}`? – jpp May 21 '18 at 14:13
-
please paste your code as text, it will help to debug the code – Gautam Rai May 21 '18 at 14:14
-
Input: {type(i) for i in df_cat_tot['cat'].values} Output: {pandas.core.series.Series, str} – Corentin Moreau May 21 '18 at 14:16
-
Did the below answer help? If so, consider [accepting](https://stackoverflow.com/help/someone-answers) (green tick on left), or ask for clarification. – jpp May 24 '18 at 22:58
-
Does this answer your question? ["Series objects are mutable and cannot be hashed" error](https://stackoverflow.com/questions/29700552/series-objects-are-mutable-and-cannot-be-hashed-error) – Trenton McKinney Aug 23 '20 at 18:11
1 Answers
2
Your series contains other pd.Series
objects. This is bad practice. In general, you should ensure your series is of a fixed type to enable you to perform manipulations without having to check for type
explicitly.
Your error is due to pd.Series
objects not being hashable. One workaround is to use a function to convert pd.Series
objects to a hashable type such as tuple
:
s = pd.Series(['one string', 'another string', pd.Series([1, 2, 3])])
def converter(x):
if isinstance(x, pd.Series):
return tuple(x.values)
else:
return x
res = s.apply(converter).unique()
print(res)
['one string' 'another string' (1, 2, 3)]
![](../../users/profiles/9209546.webp)
jpp
- 134,728
- 29
- 196
- 240