Search code examples
pandaslistappendpandas-apply

Pandas Dataframe Apply - lambda function appending to list


I have a list contained in a column of a pandas dataframe. And I want to append the value in the "price_label" column to the list.

I'm currently using a function i've made to do this, but is it the best way? I feel like I am missing something here?

My method works, but its not pretty. Any suggestions.

List before:

+---+--------------+-----------+-----------------------------------------+-------+------------------------------+-------------+
|   | prodref      | prodcateg | webproddesc                             | price | categories                   | price_label |
+---+--------------+-----------+-----------------------------------------+-------+------------------------------+-------------+
| 0 | 1004 10-14MM | STR4      | Gold Plated Bracelet 1004 - 10-14MM     | 5.98  | [Gold, Plated, Bracelet]     | 1           |
| 1 | 1004 16-22MM | STR4      | Gold Plated Bracelet 1004 - 16-22MM     | 5.98  | [Gold, Plated, Bracelet]     | 1           |
| 2 | 1007 10-14MM | STR4      | Bi-Colour Bracelet 1007 - 10-14MM       | 5.98  | [BiColour, Bracelet]         | 1           |
| 3 | 1007 16-22MM | STR4      | Bi-Colour Bracelet 1007 - 16-22MM       | 5.98  | [BiColour, Bracelet]         | 1           |
| 4 | 1010 10-14MM | STR4      | Stainless Steel Bracelet 1010 - 10-14MM | 5.98  | [Stainless, Steel, Bracelet] | 1           |
| 5 | 1010 16-22MM | STR4      | Stainless Steel Bracelet 1010 - 16-22MM | 5.98  | [Stainless, Steel, Bracelet] | 1           |
| 6 | W108/22      | STR1      | Grey Calf Watch Strap (S) - W108/22     | 4.18  | [Grey, Calf, Watch]          | 1           |
| 7 | W404/14      | STR1      | White Lizard Grain Strap (S) - W404/14  | 5.98  | [White, Lizard, Grain]       | 1           |
| 8 | W404/18      | STR1      | White Lizard Grain Strap (S) - W404/18  | 5.98  | [White, Lizard, Grain]       | 1           |
+---+--------------+-----------+-----------------------------------------+-------+------------------------------+-------------+

function :

def appendPrice(vert):
    cat_list = vert["categories"]
    cat_list.append(vert["price_label"])
    return cat_list

test["categories"] = test.apply(lambda x:appendPrice(x),axis=1)  

Output

+---+--------------+-----------+-----------------------------------------+-------+---------------------------------+-------------+
|   | prodref      | prodcateg | webproddesc                             | price | categories                      | price_label |
+---+--------------+-----------+-----------------------------------------+-------+---------------------------------+-------------+
| 0 | 1004 10-14MM | STR4      | Gold Plated Bracelet 1004 - 10-14MM     | 5.98  | [Gold, Plated, Bracelet, 1]     | 1           |
| 1 | 1004 16-22MM | STR4      | Gold Plated Bracelet 1004 - 16-22MM     | 5.98  | [Gold, Plated, Bracelet, 1]     | 1           |
| 2 | 1007 10-14MM | STR4      | Bi-Colour Bracelet 1007 - 10-14MM       | 5.98  | [BiColour, Bracelet, 1]         | 1           |
| 3 | 1007 16-22MM | STR4      | Bi-Colour Bracelet 1007 - 16-22MM       | 5.98  | [BiColour, Bracelet, 1]         | 1           |
| 4 | 1010 10-14MM | STR4      | Stainless Steel Bracelet 1010 - 10-14MM | 5.98  | [Stainless, Steel, Bracelet, 1] | 1           |
| 5 | 1010 16-22MM | STR4      | Stainless Steel Bracelet 1010 - 16-22MM | 5.98  | [Stainless, Steel, Bracelet, 1] | 1           |
| 6 | W108/22      | STR1      | Grey Calf Watch Strap (S) - W108/22     | 4.18  | [Grey, Calf, Watch, 1]          | 1           |
| 7 | W404/14      | STR1      | White Lizard Grain Strap (S) - W404/14  | 5.98  | [White, Lizard, Grain, 1]       | 1           |
| 8 | W404/18      | STR1      | White Lizard Grain Strap (S) - W404/18  | 5.98  | [White, Lizard, Grain, 1]       | 1           |
| 9 | W409/14      | STR1      | Pink Lizard Grain Strap (S) - W409/14   | 5.98  | [Pink, Lizard, Grain, 1]        | 1           |
+---+--------------+-----------+-----------------------------------------+-------+---------------------------------+-------------+

Solution

  • As @ALollz pointed out, using a list inside a a Series or DataFrame is rarely the way to go.

    That said, if you want to keep your pattern for now you could drop the appendPrice altogether:

    test["categories"] = test.apply(lambda x: x.categories + [x.price_label],axis=1) 
    

    Otherwise you could share more color on what exactly are you trying to accomplish with this.