3

I have my dictionary

d = {'A':1, 'B':2, 'C':3}

and my dataframe

df =pd.DataFrame({
"col1": ["A", "B", "C"],
"col2": [1, 2, 3],
"col3": [2, 1, 4] })

I search to compare each value in df with the correspondant value in the dictionary. If it matches the value is kept, otherwise the value is drop.

I try

m = df['col2'] >= d[df['col1']]
df.where(m, df, other = "")

But it get this error code for m: TypeError: 'Series' objects are mutable, thus they cannot be hashed...

Thank you for your help.

Billy
  • 33
  • 5

3 Answers3

1

Create a new column for comparison using apply

df[‘dict_col’] = df[‘col1’].apply(lambda k: d[k])

m = df[‘dict_col’] >= df[‘col2’]

df[‘col2’] = df[‘col2’].where(m, df, other = "")
John R
  • 1,341
  • 7
  • 17
1

You can use pd.Series.map with loc and Boolean indexing:

df = df.loc[df['col2'] >= df['col1'].map(d)]
jpp
  • 134,728
  • 29
  • 196
  • 240
1

Hint is there in error message itself.

TypeError: 'Series' objects are mutable, thus they cannot be hashed.

df['col1'] is a Series object, which is a mutable object.

Mutable objects cannot be hashed and hence cannot be used as a dictionary key. From docs:

... dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys...

You are using Series object as dictionary key. One way to rewrite d[df['col1']] is:

[d[x] for x in df['col1']]
narendra-choudhary
  • 3,837
  • 3
  • 33
  • 48
  • thanks, it's clear, I have understood my mistake (and reread the documentation). But the result of my condition is still applying to the whole row. I do not get why. – Billy Nov 13 '18 at 16:28