Skip to content
Advertisement

ffill col[c] based on col[a]==Value

I have a dataframe [pixel, total_time], i want to:

  • Make a new column “total_time_one”, which takes total_time of pixel 1 and projects it
JavaScript

I have acheved the above dataframe with :

JavaScript

Howver the code is quite long and repeats itself, is there a function better suited? or a better solution?

Also i do not undestand why if i put:

JavaScript

It doens’t work, and i have to put an extra line:

JavaScript

to make it work

Advertisement

Answer

Use where to NaN the values that aren’t 'pixel'==1 and then ffill. This technically forward fills the last value of each group, but your values are static within each group of pixels.

JavaScript

JavaScript

If you wanted to use the first value within each group (as opposed to the last), or say take the average and then ffill you can use groupby + transform. You label successive groups of 1s using the cumsum of a != comparison.

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