Skip to content
Advertisement

Python pandas decrease backfill until reach a certain number based on interval

I have the following dataframe called df,

JavaScript

I want to do a backfill when a 1 appears in any column, and fill backwards until a number appears or failing that, backfill til a set number.

So let’s say the set number to reduce o to is 0 and the decrement is 0.1, it should look like this,

JavaScript

Can this be achieved with pandas? I want to be able to set the decrement amount and the limit for example the above would be 0.1 and 0.

I know that this command can increment the values backwards:

JavaScript

But that’s not what I want.

Advertisement

Answer

You could also try using iloc to change the values based on the indices where the column value is equals to 1.0:

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement