Python / Pandas; How to hold value of column until next event

I've been struggling with a problem for a while and haven't been able to find something similar elsewhere. I'm pretty new to Python so appologies if this is pretty straight forward.

So I have a series that I put into a pandas df: series_ = [0, 0, 0, 1, 0, 0, 0, 4, 4, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 4, 0, 0] df = pd.DataFrame(series_, columns = ['Values'])

The series can have two 'signal values': Every time the series value is 1, I want the corresponding value to be 'YES' If the value is 4, it should be 'NO'.

When the series is not 1 or 4, it should replicate the previous signal value (either 'YES' or 'NO') until the next signal value. I sadly can't find a formula that fills up these empty spots.

So what I currently have is my series ('Values') and the translation of these signal values in the column ('Modified'). I am trying to find a formula that will get me the column ('Expected'), a formula which basically replaces the 'UNDECLARED' values with either 'YES' or 'NO' based on the last available signal value. You can see this in the image I posted below.

enter image description here

Would appreciate all help!

Thanks in advance.


Read more here:

Content Attribution

This content was originally published by Tom909090 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: