如何找到分组数据中上面每一行的总和?

时间:2019-01-14 15:17:26

标签: python pandas numpy

我有一个df

 AccountID       PurchaseDate                 Price
    | 113        2018-09-01 22:56:30              13|
    | 114        2018-09-03 22:57:30              23|
    | 113        2018-09-02 22:56:30              19|
    | 114        2018-09-01 22:56:30              20|
    | 114        2018-09-03 22:56:30              25|

我的AccountID已经在groupby()中 如何创建新列TotalPurchase,其中包含Price的总和,但仅适用于同一行前一天的同一AccountIDPurchaseDate

AccountID       PurchaseDate                 Price          TotalPurchase
| 113        2018-09-01 22:56:30              13               0  |
| 113        2018-09-02 22:56:30              19               13 |
| 114        2018-09-01 22:56:30              20               0  |
| 114        2018-09-03 22:56:30              25               20 |
| 114        2018-09-03 22:57:30              23               45 |

1 个答案:

答案 0 :(得分:0)

使用params.require(:user_params).permit(:name, :age, :city) library(tidyverse) df <- data.frame(group = factor(c("overall", "low-income", "high-income"), levels = c("overall", "low-income", "high-income")), AUC = c(0.70, 0.60, 0.80)) ggplot(df) + geom_col(aes(x=group, y=AUC)) + theme(axis.title.x=element_blank()) 尝试以下操作:

shift()

输出:

cumsum()