mtm1186
mtm1186

Reputation: 135

Replace Boolean value with name of df column if value is true Pandas

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

Upvotes: 0

Views: 1964

Answers (2)

SeaBean
SeaBean

Reputation: 23217

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

Upvotes: 1

ALollz
ALollz

Reputation: 59519

We can use the fact that True evaluates to 1 and False evaluates to 0, together with string multiplication by 1 returning the string while multiplication by 0 returns the empty string.

Either supply a list of the columns or select them programatically somehow. Below I select all of the 'bool' columns for which the multiplication will work.

# Get all Boolean columns
cols = df.head().select_dtypes('bool').columns

df.loc[:, cols] = df.loc[:, cols].mul(cols)

print(df)
#        Acct Name  product 1  Product 2
#0  csld solutions  product 1           
#1  bolo construct             Product 2

Upvotes: 2

Related Questions