In Python (Pandas / Numpy), how to multiply df using a condition and a specific block size?
I have df
A = pd.DataFrame([[1, 5, 2, 0], [2, 4, 4, 0], [3, 3, 1, 1], [4, 2, 2, 0], [5, 1, 4, 0], [2, 4, 4, 0], [3, 3, 1, 1], [4, 2, 2, 0], [5, 1, 4, 0]],
columns=['A', 'B', 'C', 'D'], index=[1, 2, 3, 4, 5, 6, 7, 8, 9])
I want to be able to subset the data according to the following rules: Select the rows for which the "D" column value is 1 and also the two above them (Chunk Size = 3).
If I apply the rule in the df example, the output should be:
A B C D
1 1 5 2 0
2 2 4 4 0
3 3 3 1 1
5 5 1 4 0
6 2 4 4 0
7 3 3 1 1
thank
+3
source to share