I want to append 2 Rows (Yes/No) for Each Unique Session name.
Eg: Take 1st Session
I want to Add 2 Rows Yes and No which comprises of values as stated below
Yes -> “On Duty + Attended + Online Prescence” => 25+30+40 = 95
No -> “Did Not Attend => 10.
Is there any way to do it in Python.
My input is given below
—————————————
My Final output Should Look like this
Advertisement
Answer
Here is an idea via dplyr
with only fault that the Yes
and No
appear at the top of each group,
JavaScript
x
11
11
1
library(dplyr)
2
3
df %>%
4
group_by(Name, grp = cumsum(Status == 'Did not attend')) %>%
5
summarise(count = sum(count)) %>%
6
ungroup() %>%
7
select(-grp) %>%
8
mutate(Status = rep(c('Yes', 'No'), length(unique(Name)))) %>%
9
bind_rows(df) %>%
10
arrange(Name)
11
which gives,
JavaScript121211# A tibble: 17 x 3
2Name count Status
3<dbl> <dbl> <chr>
41 1 95 Yes
52 1 10 No
63 1 25 On Duty
74 1 30 Attended
85 1 40 Online Presence
96 1 10 Did not attend
107 2 110 Yes
118 2 20 No
129 2 20 On Duty
1310 2 50 Attended
1411 2 40 Online Presence
1512 2 20 Did not attend
1613 3 26 Yes
1714 3 11 No
1815 3 11 On Duty
1916 3 15 Attended
2017 3 11 Did not attend
21
DATA:
JavaScript
1
7
1
dput(df)
2
structure(list(Name = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3), Status = c("On Duty",
3
"Attended", "Online Presence", "Did not attend", "On Duty", "Attended",
4
"Online Presence", "Did not attend", "On Duty", "Attended", "Did not attend"
5
), count = c(25, 30, 40, 10, 20, 50, 40, 20, 11, 15, 11)), class = "data.frame", row.names = c(NA,
6
-11L))
7