I have a dataframe and a list. The dataframe consists of the column 'CODE' and 'distance'. The list consists of a row of values. I want the value in 'CODE' changed to zero if values from the list are also present in the distance column of the dataframe.
import pandas as pd
df = pd.DataFrame({'CODE': np.arange(0, 220),
'distance': np.arange(0, 1100, 5)})
a = [1080, 1090] #list
I can do the above operation comparing the dataframe with a single value using the code below.
df.loc[df.distance == 1080, 'CODE'] = 0
This results in the following outcome:
CODE distance
3 3 15
4 4 20
.. ... ...
215 215 1075
216 0 1080
217 217 1085
218 218 1090
However, when I try to replace the value 1080 with the list a (see below) it doesn't work. How can I solve this?
df.loc[df.distance == a, 'CODE'] = 0