gpt4 book ai didi

python - 乘法追加python

转载 作者:行者123 更新时间:2023-12-05 01:12:18 28 4
gpt4 key购买 nike

我有一个 Pandas 数据框

   Colour   Type    Cost    Price   Type
0 Red Car 3 5 Standard
1 Blue Bike 6 7 Standard
2 Blue Car 4 8 Standard
3 Green Bike 6 9 Standard
4 Yellow Bike 3 3 Standard

然后我有一个调整系列,我想将其乘以成本并添加到底部

Red   2
Blue 1
Green 3

所以输出是:

   Colour   Type    Cost    Price   Type
0 Red Car 3 5 Standard
1 Blue Bike 6 7 Standard
2 Blue Car 4 8 Standard
3 Green Bike 6 9 Standard
4 Yellow Bike 3 3 Standard
0 Red Car 6 10 Adjusted
1 Blue Bike 6 7 Adjusted
2 Blue Car 4 8 Adjusted
3 Green Bike 18 27 Adjusted

我有点迷茫,有没有一种简单的方法可以做到这一点?

最佳答案

您可以使用mergeconcat

调整数据框:

   Colour   value
0 Red 2
1 Blue 1
2 Green 3

然后:

temp = df.merge(adj)

temp["Cost"] = temp["Cost"]*temp["value"]
temp["Price"] = temp["Price"]*temp["value"]
temp["Type.1"] = ["Adjusted"]*temp.shape[0]


pd.concat([df, temp.iloc[:,0:5]], axis=0)

输出:

    Colour  Type    Cost    Price   Type.1
0 Red Car 3 5 Standard
1 Blue Bike 6 7 Standard
2 Blue Car 4 8 Standard
3 Green Bike 6 9 Standard
4 Yellow Bike 3 3 Standard
0 Red Car 6 10 Adjusted
1 Blue Bike 6 7 Adjusted
2 Blue Car 4 8 Adjusted
3 Green Bike 18 27 Adjusted

关于python - 乘法追加python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62279239/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com