我在pandas数据框的列中包含一个列表。我想将“ price_label”列中的值附加到列表中。
我目前正在使用自己制作的函数来执行此操作,但这是最好的方法吗?我觉得我在这里想念什么吗?
我的方法有效,但效果不佳。有任何建议。
之前列出:
+---+--------------+-----------+-----------------------------------------+-------+------------------------------+-------------+
| | 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 |
+---+--------------+-----------+-----------------------------------------+-------+------------------------------+-------------+
功能:
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)
输出
+---+--------------+-----------+-----------------------------------------+-------+---------------------------------+-------------+
| | 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 |
+---+--------------+-----------+-----------------------------------------+-------+---------------------------------+-------------+
正如@ALollz指出的那样,很少使用Series或DataFrame中的列表。
就是说,如果您现在想保留模式,可以将appendPrice
完全删除:
test["categories"] = test.apply(lambda x: x.categories + [x.price_label],axis=1)
否则,您可以在尝试完成此操作时共享更多颜色。