I have a working R code to slice rows based on modulus condition, I am trying to achieve same thing in python
Any help will be really appreciated.
R Code:
data = read.table(text = 'id a b
1 2 0
1 3 0
1 0 0
1 0 1
2 1 0
2 1 1
2 1 0
3 0 1
3 0 1
3 0 1
3 1 0
3 1 1
3 0 1',header=T)
library(dplyr)
data <- data %>%
group_by(id) %>%
dplyr::slice(if(n()%% 3 == 0) row_number() else seq_len(n()-1))
Expected Python Output:
id a b
1 2 0
1 3 0
1 0 0
2 1 0
2 1 1
2 1 0
3 0 1
3 0 1
3 0 1
3 1 0
3 1 1
3 0 1