Reputation: 33
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.
Upvotes: 3
Views: 166
Reputation: 1508
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 = "")
Upvotes: 1
Reputation: 4826
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']]
Upvotes: 1
Reputation: 164823
You can use pd.Series.map
with loc
and Boolean indexing:
df = df.loc[df['col2'] >= df['col1'].map(d)]
Upvotes: 1