0

I'm trying to write a program to get some specific data. I got a column that can have integer numbers (negative, positive and zeros), I wanted to count the consecutive zeros before getting a negative or positive. I succesfully got (with help) to code that. But I get a column adding the previous number and cumulating the sum, but I only need the biggest number before finding a zero.

I got this code to check cumulative sum for the zero values, this count restarts when finds a zero.

a = df['Amperaje'].shift().eq(0)
b = a.cumsum()
df['DT_Count'] = b.sub(b.mask(a).ffill().fillna(0))
print (df)

With this for example if my base column has: df['Amperaje']= [0,0,-53,-25,6,0,0,0,5] I get df['DT_Count']= [1,2,3,0,0,0,0,1,2,3,4]

But I only want to have the 3 then the 4, the rest of the list 0.

SuperStormer
  • 4,997
  • 5
  • 25
  • 35
  • Is this related to pandas? Please tag your question appropriately or add details about the library you are using. It's not clear from where df is coming. – Ankush Chavan Aug 18 '23 at 04:24
  • ***Do not include pictures of your data. Include your data by running df.to_dict() or df.head().to_dict().*** See: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors – Mark Aug 18 '23 at 12:24
  • Btw welcome to StackOverflow! – Mark Aug 18 '23 at 12:34
  • df['Amperaje']= [0,0,-53,-25,6,0,0,0,5] has 9 elements, df['DT_Count']= [1,2,3,0,0,0,0,1,2,3,4] has 11 – Mark Aug 18 '23 at 12:34

0 Answers0