Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23160

Group rows with preceeded by 0 in Pandas

$
0
0

I have a requirment to group rows preceeding by 0 in column A and sum up the values in column B. For rows that are not prceeded by 0, print the same in output. How can i acheive this using pandas dataframe. I have given sample grouping with expected output in the attached screenshot. even if there are rows 20 preceding rows it should group

enter image description here

Reproducible input:

df = pd.DataFrame({'Column A': [0,0,1,1,1,0,-1,-1,0,0,0,1,0,0,0,0,-1],'Column B': [5,3,4,2,1,3,7,5,10,2,3,4,5,5,5,5,5]                  })

Viewing all articles
Browse latest Browse all 23160

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>