Pandas Forward Backward fill on columns within column level

Viewed 912

I have the following dataframe: df

                     name  width  length
timestamp                           
2019-08-01 00:00:08    10   10.0     NaN
2019-08-01 00:00:19    10    NaN     NaN
2019-08-01 00:00:56    10    NaN     86.0
2019-08-01 00:00:08    12    NaN     90
2019-08-01 00:00:19    12   12.0     NaN
2019-08-01 00:00:28    12    NaN     NaN

I would like to apply forward and backward fill on the columns 'width' and 'length' within for the column 'name'. The result would look like this:

                     name  width  length
timestamp                           
2019-08-01 00:00:08    10   10.0     86
2019-08-01 00:00:19    10   10.0     86
2019-08-01 00:00:56    10   10.0     86
2019-08-01 00:00:08    12   12.0     90
2019-08-01 00:00:19    12   12.0     90
2019-08-01 00:00:28    12   12.0     90

Any ideas how to do this?

2 Answers

We need groupby with apply , since we chain two functions ffill and bfill together

df.update(df.groupby('name').apply(lambda x : x.ffill().bfill()))

as you said each unique name has only one value of width and length, you may be able to avoid apply by using transform and max or first

df.update(df.groupby('name')[['width','length']].transform('max'))

Out[87]:
                     name  width  length
timestamp
2019-08-01 00:00:08    10   10.0    86.0
2019-08-01 00:00:19    10   10.0    86.0
2019-08-01 00:00:56    10   10.0    86.0
2019-08-01 00:00:08    12   12.0    90.0
2019-08-01 00:00:19    12   12.0    90.0
2019-08-01 00:00:28    12   12.0    90.0
Related