How to check if a value is in 1 or 2 list-columns with pandas

Viewed 30

I build a pandas dataframe from json data :

    {
    'bundle': 'R_FLUSH_DEADLETTERS',
    'envs': ['AG','DEV','QUAL','QUAL2','PREPROD','PREPROD2','PROD'],
    'envsinfra2021': ['PREPROD2'],
    },
    {
    'bundle': 'R201_QA069_ETIQETTENS_FROMSAP',
    'envs': ['DEV','QUAL','QUAL2','PREPROD'],
    'envsinfra2021': ['AG','PREPROD2','PROD'],
},

I can retrieve the rows that contain 'PROD' in the 'envsinfra2021' list-column with

df[['PROD' in x for x in df["envsinfra2021"]]]

I can retrieve the rows that contain 'PROD' in the 'envs' list-column with

df[['PROD' in x for x in df["envs"]]]

But how can I get the rows where 'PROD' is either in 'envs' or 'envsinfra2021' ? I tried this

df[ ['PROD' in x for x in df["envs"]] or ['PROD' in x for x in df["envsinfra2021"]] ]

but it returns an empty DF

1 Answers

Convert list to numpy arrays and add | for bitwise OR:

df = df[ np.array(['PROD' in x for x in df["envs"]]) |
         np.array(['PROD' in x for x in df["envsinfra2021"]])]
print (df)
                bundle                                             envs  \
0  R_FLUSH_DEADLETTERS  [AG, DEV, QUAL, QUAL2, PREPROD, PREPROD2, PROD]   

  envsinfra2021  
0    [PREPROD2]  

Another idea is chain scalars, so used or:

df = df[['PROD' in x or 'PROD' in y for x, y in zip(df["envs"], df["envsinfra2021"])]]
Related