I have a list, X
, that contains a set of legal values for a column. Say, I have column A
. I want to replace (set to empty string) elements in df['A']
if their value is not in X. How can I do that efficiently in Pandas?
I know there is isin()
, but that just checks if the values are present and returns a Series of True/False.
try this:
df.loc[~df.A.isin(X), 'A'] = ''
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With