Difference-based filtering data frames have two series, one mapped by a dictionary… here is a solution to the problem.
Difference-based filtering data frames have two series, one mapped by a dictionary
I have my dictionary
d = {'A':1, 'B':2, 'C':3}
and My Data Frame
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 corresponding value in the dictionary. The value is retained if there is a match, otherwise it is discarded.
I’ll try
m = df['col2'] >= d[df['col1']]
df.where(m, df, other = "")
But it gets the error code M: TypeError: ‘Series’ objects are mutable, thus they cannot be hashed….
Thanks for your help.
Solution
Use apply to create a new column for comparison
df[‘dict_col’] = df[‘col1’].apply(lambda k: d[k])
m = df[‘dict_col’] >= df[‘col2’]
df[‘col2’] = df[‘col2’].where(m, df, other = "")