Reputation: 11
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.
Would appreciate all help!
Thanks in advance.
Tom
Upvotes: 0
Views: 367
Reputation: 61
I am certain, that there is a better way to do this but here is a quick and dirty solution, not sure if speed is important to you...
df['Expected']=df['Values'].replace(0,np.NaN).ffill().replace({1:'NO', 4: 'YES'}).fillna('UNDECLARED')
Upvotes: 2