user4979733
user4979733

Reputation: 3411

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

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.

Upvotes: 6

Views: 8617

Answers (2)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210812

You can use the standard Pandas indexing here:

df.loc[~df.A.isin(X), 'A'] = ''

~df.A.isin(X) - will revert the boolean Series returned by df.A.isin(X) (i.e. False -> True and True -> False )

Upvotes: 18

Jeff
Jeff

Reputation: 2228

You can do it with apply:

import pandas as pd

x = ['a', 'b', 'c']
data = {'foo':['a', 'a', 'q', 'p']}
df = pd.DataFrame.from_dict(data)

df_new = df['foo'].apply(lambda i: i if i in x else '')

Upvotes: 4

Related Questions