I have a datafarme which looks like as follows (there are more columns having been dropped off):
memberID shipping_country
264991
264991
You can use GroupBy
+ ffill
/ bfill
:
def filler(x):
return x.ffill().bfill()
res = df.groupby('memberID')['shipping_country'].apply(filler)
A custom function is necessary as there's no combined Pandas method to ffill
and bfill
sequentially.
This also caters for the situation where all values are NaN
for a specific memberID
; in this case they will remain NaN
.