Skip to content Skip to sidebar Skip to footer

How To Merge Cells Partially Along With Vertical Direction In Pandas

I have the following dataframe. I would like to get a vertical Mergedata which is separated by Flag 1. Maybe, I can do it with FOR LOOP. However, I believe that pandas provide mo

Solution 1:

We just need cumsum Flag to get the groupby key

df.loc[df.Flag==1,'newmerge'] = df.groupby(df.Flag.cumsum())['Data'].sum().values
df
Out[47]: 
   index  Flag  Data          newmerge
0      0     1  aaaa  aaaabbbbccccdddd
1      1     0  bbbb               NaN
2      2     0  cccc               NaN
3      3     0  dddd               NaN
4      4     1  eeee          eeeeffff
5      5     0  ffff               NaN
6      6     1  gggg              gggg
7      7     1  hhhh              hhhh
8      8     1  iiii              iiii

Post a Comment for "How To Merge Cells Partially Along With Vertical Direction In Pandas"