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