2

I have a dataframe in which I construct a list of values as such:

data_df["items"] = data_df.apply(
    lambda x: [x["item_1"], x["item_2"]]
    if x["item_2"]
    else [x["item_1"]],
    axis=1,
)

In my mind this approach should construct a list containing items 1 and 2 if item_2 exist otherwise it should just construct a list containing item_1. However, the check on item_2 doesn't seem to work as expected. I have tried checking for None values as well as empty strings. How do I write this so that the following happens:

if item_1 and item_2 are None or "" return []

if item_1 is not None but item_2 is None or "" return [item_1]

if item_1 and item_2 return [item_1, item_2]

Also, item_1 and item_2 are strings

00robinette
  • 497
  • 5
  • 16
  • [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii May 21 '21 at 13:20

1 Answers1

2

use pd.isna():

df['item_list'] = df[['item1', 'item2']].apply(lambda x: [i for i in x if not pd.isna(i)], 1)

update:

df['item_list'] = df[['item1', 'item2']].apply(lambda x: [i for i in x if not (pd.isna(i) or i == '')], 1)
Nk03
  • 14,699
  • 2
  • 8
  • 22