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 a
Solution 1:
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
Solution 2:
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 20 csld solutions product 1None1 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
Post a Comment for "Replace Boolean Value With Name Of Df Column If Value Is True Pandas"