Assuming we have a df as follows
df = pd.DataFrame({ 'Col1' : [1, 1, 1, 2, 2, 2, 2],
'Col2' : [5, 6, 8, 3, 7, 8, 5],
'Col3' : [2, None, None, 3, None, None, 4],
'Col4' : [3, None,5, None, 8, None, 66],
'Col5': [None, 8, 6, None, 9, 6,None],
'Col6' : [3,5,2,5,2,7,9]})
I wanted to replace the None
values in the columns Col3
, Col4
and Col5
using the solution suggested by jjs in this post here after applying groupby
on the first column Col1
.
The way I did is
df = df.groupby('Col1')['Col3','Col4','Col5'].ffill().bfill()
but it is a lot of work for mentioning the columns manually.
So, I wanted to know how can I choose the columns Col3
, Col4
and Col5
by slicing?
Thanks