如何根据上述行的值添加新列 [英] how to add new column based on the above row's value

查看:69
本文介绍了如何根据上述行的值添加新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,如下所示.首先,它们具有三列(日期",时间",标志").我想根据标志和日期添加一列,这意味着当我获得flag = 1时,那么今天剩下的目标是1,否则目标是零.

I have one dataframe as below. At first,they have three columns('date','time','flag'). I want to add one column which based on the flag and date which means when I get flag=1 ,then the rest of this day the target is 1, otherwise the target is zero.

  date        time      flag  target
0 2017/4/10   10:00:00  0     0
1 2017/4/10   11:00:00  1     1
2 2017/4/10   12:00:00  0     1
3 2017/4/10   13:00:00  0     1
4 2017/4/10   14:00:00  0     1
5 2017/4/11   10:00:00  1     1
6 2017/4/11   11:00:00  0     1
7 2017/4/11   12:00:00  1     1
8 2017/4/11   13:00:00  1     1
9 2017/4/11   14:00:00  0     1
10 2017/4/12  10:00:00  0     0
11 2017/4/12  11:00:00  0     0
12 2017/4/12  12:00:00  0     0
13 2017/4/12  13:00:00  0     0
14 2017/4/12  14:00:00  0     0
15 2017/4/13  10:00:00  0     0
16 2017/4/13  11:00:00  1     1
17 2017/4/13  12:00:00  0     1
18 2017/4/13  13:00:00  1     1
19 2017/4/13  14:00:00  0     1

推荐答案

使用

Use DataFrameGroupBy.cumsum for cumulative sum flag values, compare with 0 and last cast mask to integer:

df['new'] = (df.groupby('date')['flag'].cumsum() > 0).astype(int)
print (df)
         date      time  flag  target  new
0   2017/4/10  10:00:00     0       0    0
1   2017/4/10  11:00:00     1       1    1
2   2017/4/10  12:00:00     0       1    1
3   2017/4/10  13:00:00     0       1    1
4   2017/4/10  14:00:00     0       1    1
5   2017/4/11  10:00:00     1       1    1
6   2017/4/11  11:00:00     0       1    1
7   2017/4/11  12:00:00     1       1    1
8   2017/4/11  13:00:00     1       1    1
9   2017/4/11  14:00:00     0       1    1
10  2017/4/12  10:00:00     0       0    0
11  2017/4/12  11:00:00     0       0    0
12  2017/4/12  12:00:00     0       0    0
13  2017/4/12  13:00:00     0       0    0
14  2017/4/12  14:00:00     0       0    0
15  2017/4/13  10:00:00     0       0    0
16  2017/4/13  11:00:00     1       1    1
17  2017/4/13  12:00:00     0       1    1
18  2017/4/13  13:00:00     1       1    1
19  2017/4/13  14:00:00     0       1    1

这篇关于如何根据上述行的值添加新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆