The Python Oracle

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

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Melt

--

Chapters
00:00 Question
00:40 Accepted answer (Score 17)
01:04 Answer 2 (Score 3)
01:19 Thank you

--

Full question
https://stackoverflow.com/questions/3800...

Accepted answer links:
[standard Pandas indexing]: https://pandas.pydata.org/docs/user_guid...
[df.A.isin(X)]: https://pandas.pydata.org/docs/reference...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas

#avk47



ACCEPTED ANSWER

Score 18


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 )




ANSWER 2

Score 4


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 '')