0

I have a DataFrame like below and would like for B to be 1 for n rows after the 1 in column A (where below n = 2)

index  A   B
0      0   0
1      1   0
2      0   1
3      0   1
4      1   0
5      0   1
6      0   1
7      0   0
8      1   0
9      0   1

I think I can do it using .ix similar to this example but not sure how. I'd like to do it in a single in pandas-style selection command if possible. (Ideally not using rolling_apply.) Modifying a subset of rows in a pandas dataframe

EDIT: the application is that the 1 in column A is "ignored" if it falls within n rows of the previous 1. As per the comments, for n = 2 then, and these example:

A = [1, 0, 1, 0, 1], B should be = [0, 1, 1, 0, 0] A = [1, 1, 0, 0], B should be [0, 1, 1, 0]

Community
  • 1
  • 1
user1387717
  • 1,039
  • 1
  • 13
  • 30
  • What if you have `1, 0, 1, 0, 1` or `1, 1, 0, 0` in column `A`, how do you want B to be? – Psidom Dec 14 '16 at 22:35
  • Good question: ideally if it was [1, 0, 1, 0, 1] and n=2, then B would be [0, 1, 1, 0, 0] (i.e. the first 1 in column A needs to propagate its 2 more 1's but then the 1 in column A index=2, is "knocked out" b/c there is a one already in column B for that index) – user1387717 Dec 14 '16 at 22:38
  • For your second example A = [1,1,0,0], I would ideally like B to be = [0, 1, 1, 0] if n = 2 – user1387717 Dec 14 '16 at 22:39

0 Answers0