Skip to content
Advertisement

Conditionally replace values in list of arrays in a pandas dataframe

I would like to conditionally replace values in a column that contains a series of arrays.

Example dataset below: (my real dataset contains many more columns and rows)

JavaScript

For example, if the condition is A and the row contains ‘1 pumpkin’, then I would like to replace the value with XXX. But if the condition is B and the row contains 1 pumpkin, then I would like to replace the value with YYY.

Desired output

JavaScript

The goal is, in fact, to replace all these values but 1 pumpkin is just one example. Importantly, I would like to maintain the array structure. Thanks!

Advertisement

Answer

Let us do explode then np.select

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement