Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Replace column values to empty if not present in pre-defined list

Tags:

python

pandas

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.

like image 534
user4979733 Avatar asked Dec 08 '22 22:12

user4979733


1 Answers

try this:

df.loc[~df.A.isin(X), 'A'] = ''
like image 96
MaxU - stop WAR against UA Avatar answered Mar 15 '23 23:03

MaxU - stop WAR against UA