[SOLVED] Create a column containing columns' names


I have a df of orders where for each product there’s an indicator whether it was bought or not (0/1). I want to add a new column called total_by column that will concatenate only the bought products per each order.
I tried different apply techniques, but didn’t succeed to get it right.
Basically, I need to strip the "was_" and "_bought?" before adding to total buy and it should be done by condition of value=1 per each product. In the example below, I have 3 products, in reality there are many more, so I can’t just write it explicitly

order_id  was_pencil_bought?  was_notebook_bought?  was_bag_bought   total_buy
     1            1                    0                   1        'pencil','bag'
     2            0                    0                   1        'bag'
     3            1                    1                   1        'pencil','notebook','bag'


This should work:

selected_cols = ["was_pencil_bought", "was_notebook_bought", "was_bag_bought"]
def func(data):
    l = []
    for col in selected_cols:
        if data[col] == 1:
    return l
df["total_buy"] = df.apply(func, axis=1)

Answered By – Prawigya

Answer Checked By – Willingham (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *