I want to replace the values in a pandas dataframe if they are contained in a set. Here is a mini-example:
lset = frozenset((0,1))
data = {'col1': [3, 2, 1, 0]}
df = pd.DataFrame.from_dict(data)
I want all the values of col1
contained in lset
to be replaced by -5. The result would be equivalent to:
data = {'col1': [3, 2, -5, -5]}
df = pd.DataFrame.from_dict(data)
I tried
df.loc[df['col1'] in lset, 'col1'] = -5
but I'm getting TypeError: unhashable type: 'Series'