【发布时间】:2020-09-25 14:08:42
【问题描述】:
我有 DataFrame,它由 3 列组成:CustomerId、Amount 和 Status(成功或失败)。 DataFrame 没有以任何方式排序。一个 CustomerId 可以在 DataFrame 中重复多次。
我想用以下逻辑在这个 DataFrame 中引入新列:
df[totalamount]= 状态为成功的每个客户的金额总和。
我已经有一个正在运行的代码,但是使用 df.iterrows 需要太多时间。因此请求您提供替代方法,如 pandas 矢量化或 numpy 矢量化。
例如,我想从前三列创建“totalamount”列:
CustomerID Amount Status totalamount
0 1 5 Success 105 # since both transatctions were successful
1 2 10 Failed 80 # since one transaction was successful
2 3 50 Success 50
3 1 100 Success 105
4 2 80 Success 80
5 4 60 Failed 0
【问题讨论】:
标签: python-3.x pandas dataframe data-analysis