我在熊猫数据框的一列中有一个列表。我想将“ 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 |
+---+--------------+-----------+-----------------------------------------+-------+---------------------------------+-------------+
答案 0 :(得分:2)
正如@ALollz所指出的那样,很少使用Series或DataFrame中的列表。
也就是说,如果您现在想要保留模式,则可以完全删除appendPrice
:
test["categories"] = test.apply(lambda x: x.categories + [x.price_label],axis=1)
否则,您可以在尝试完成此操作时共享更多颜色。
答案 1 :(得分:1)
假设
df["categories"]
0 [Gold, Plated, Bracelet]
1 [Gold, Plated, Bracelet]
2 [BiColour, Bracelet]
3 [BiColour, Bracelet]
4 [Stainless, Steel, Bracelet]
5 [Stainless, Steel, Bracelet]
6 [Grey, Calf, Watch]
7 [White, Lizard, Grain]
8 [White, Lizard, Grain]
Name: categories, dtype: object
和df['price_label']
作为int
,将执行以下操作:
df.apply(lambda row: row["categories"] + list(str(row["price_label"])), axis=1)
0 [Gold, Plated, Bracelet, 1]
1 [Gold, Plated, Bracelet, 1]
2 [BiColour, Bracelet, 1]
3 [BiColour, Bracelet, 1]
4 [Stainless, Steel, Bracelet, 1]
5 [Stainless, Steel, Bracelet, 1]
6 [Grey, Calf, Watch, 1]
7 [White, Lizard, Grain, 1]
8 [White, Lizard, Grain, 1]
dtype: object
答案 2 :(得分:1)
这行代码将满足您的要求。它将price_label列中的值附加到每个类别行的末尾。对于较大的数据,此操作比df.apply()更有效。
Person