Home > front end >  Group by aggregate elements so far in the same group - Pandas
Group by aggregate elements so far in the same group - Pandas

Time:10-19

Here's an example of how the output should look like:

Dataframe: df with required output

class_id    item    req_output
a           1       [1] 
a           2       [1,2]   
a           3       [1,2,3]
b           1       [1] 
b           2       [1,2]

I've tried: df.groupby("class").apply(lambda x: list(x["item"])

class_id    output
a           [1,2,3]
b           [1,2]

but this only gives the whole aggregation, however I need the aggregation to happen in every row considering the class

CodePudding user response:

First, make each element into a list of size 1. Here, we are (exploiting?) the fact [1] [2] = [1, 2]. Then group by and GroupBy.apply Series.cumsum.

df["req_output"] = (
    df["item"]
    .map(lambda x: [x])
    .groupby(df["class_id"])
    .apply(lambda x: x.cumsum())
)

  class_id  item req_output
0        a     1        [1]
1        a     2     [1, 2]
2        a     3  [1, 2, 3]
3        b     1        [1]
4        b     2     [1, 2]

Or we can make a function to return the desired list and use GroupBy.transform.

def get_slices(s):
    """
    >>> get_slices( pd.Series([1, 2, 3]) )
    [[1], [1, 2], [1, 2, 3]]
    """
    lst = s.tolist()
    return [lst[:i] for i in range(1, len(lst) 1)]

df['req_output'] = df.groupby('class_id')['item'].transform(get_slices)
  • Related