Home > Back-end >  Cumulative concatenation from last to first within a group in Python
Cumulative concatenation from last to first within a group in Python

Time:01-16

I'm looking to concatenate in cumulative manner values within a column in a data frame. However, the column will be partitioned/grouped by the values in another column.

I have been able to do this from the top down with the following code:

df['Col_to_cum_Concat']=[y.CUM_CONCAT_TOP.tolist()[:z 1] for x, y in df.groupby('Group_Col')for z in range(len(y))]
df['Col_to_cum_Concat'] = df['Col_to_cum_Concat'].astype(str).str.lower()

Is there an easier way to go from last to first row within the group?

Example: Group_Col Text
1
1
2
2
2
3

I have tried the code below but is not exactly working.

df['Col_to_cum_Concat']=[y.CUM_CONCAT_TOP.tolist()[z:] for x, y in df.groupby('Group_Col')for z in range(len(y))]
df['Col_to_cum_Concat'] = df['Col_to_cum_Concat'].astype(str).str.lower()

Also, I apologize in advance if this is a dumb question. I'm still a newbie at Python.

CodePudding user response:

You can group by Group_Col and for each group, reverse Text and use cumsum to concatenate accumulatively:

df['Col_to_cum_Concat'] = df.Text.groupby(df.Group_Col).transform(lambda g: g[::-1].add(' ').cumsum()).str.rstrip()

df
   Group_Col Text Col_to_cum_Concat
0          1    B               A B
1          1    A                 A
2          2    C             A B C
3          2    B               A B
4          2    A                 A
5          3    B               A B
6          3    A                 A

Data:

df = pd.DataFrame({'Group_Col': [1,1,2,2,2,3,3], 'Text': ['B', 'A', 'C', 'B', 'A', 'B', 'A']})
  •  Tags:  
  • Related