running-count
Add a sequential counter column on groups to a pandas dataframe
use cumcount(), see docs here In [4]: df.groupby([‘c1’, ‘c2’]).cumcount() Out[4]: 0 0 1 1 2 0 3 1 4 0 5 1 6 2 7 0 8 0 9 0 10 1 11 2 dtype: int64 If you want orderings starting at 1 In [5]: df.groupby([‘c1’, ‘c2’]).cumcount()+1 Out[5]: 0 1 1 2 2 1 3 … Read more
Numbering rows within groups in a data frame
Use ave, ddply, dplyr or data.table: df$num <- ave(df$val, df$cat, FUN = seq_along) or: library(plyr) ddply(df, .(cat), mutate, id = seq_along(val)) or: library(dplyr) df %>% group_by(cat) %>% mutate(id = row_number()) or (the most memory efficient, as it assigns by reference within DT): library(data.table) DT <- data.table(df) DT[, id := seq_len(.N), by = cat] DT[, id … Read more