I have a dataframe that has Boolean values representing which products each customer owns. How can I replace the Boolean value with the name of the df column if the value is True and change values to null if False?

ex.

input

Acct Name | product 1 | Product 2 |
---|---|---|

csld solutions | TRUE | FALSE |

bolo construct | FALSE | TRUE |

output

Acct Name | product 1 | Product 2 |
---|---|---|

csld solutions | product 1 | |

bolo construct | Product 2 |

## Answer

You can use `np.where()`

on each column with the help of `.apply()`

to go through each column. In `np.where()`

if x is True, set to `x.name`

which contains the column name, if x is False, set to `None`

.

df.set_index('Acct Name').apply(lambda x: np.where(x, x.name, None)).reset_index()

**Result:**

Acct Name product 1 Product 2 0 csld solutions product 1 None 1 bolo construct None Product 2

If you want blank/null string instead of `None`

, you can use:

df.set_index('Acct Name').apply(lambda x: np.where(x, x.name, '')).reset_index()

**Result:**

Acct Name product 1 Product 2 0 csld solutions product 1 1 bolo construct Product 2