Going fast! #DataquestChallenge Premium Annual Offer:
500 get 50% & the next 1000 get 40% off.
GET OFFER CODE

Write a function in python that generates a single column named as "content type" from all label enceoded column

Like I have certain label encoded columns with 1 and 0. now I want to create a column which consists of column names of all the columns which has label encoded 1, the output is given below

“”"
action baseball filter western contenttype(output)
1 0 1 0 action,filter
0 1 0 1 baseball,western
0 1 1 1 baseball,filter,western
“”"

Hi @sakethsai32:

By providing more information, I would be able to refer you to more specific examples (a.k.a. your existing code and the libraries that you are making use of/are restricted to). Based on the information given, you may find the documentation below useful.

https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html

Hi @sakethsai32,

df = pd.DataFrame(np.array([[1,0,1,0],[0,1,0,1],[0,1,1,1]]),columns=['action','baseball','filter','western'])
vals = []
for i in range(0,len(df)):
    vals.append(df[df==1].T[i].nlargest(4).index)
vals = [list(v) for v in vals]
df['contenttype(output)'] = vals
df['contenttype(output)'] = df['contenttype(output)'].apply(lambda x: ",".join(x))
df
action baseball filter western contenttype(output)
0 1 0 1 0 action,filter
1 0 1 0 1 baseball,western
2 0 1 1 1 baseball,filter,western
1 Like