python - How to replace NaNs with valid value from within Pandas group -


i want replace nans in pandas dataframe column non-nan values within same group. in case these geo coordinates reason data points lookup failed. e.g.:

df.groupby('place') 

looks

place| lat | lng ----------------- foo  | nan | nan foo  | 1   | 4 foo  | 1   | 4 foo  | nan | nan bar  | 5   | 7 bar  | 5   | 7 bar  | nan | nan bar  | nan | nan bar  | 5   | 7 

==> want:

foo  | 1   | 4 foo  | 1   | 4 foo  | 1   | 4 foo  | 1   | 4 bar  | 5   | 7 bar  | 5   | 7 bar  | 5   | 7 bar  | 5   | 7 bar  | 5   | 7 

in case lat/lng values within same 'place' grouping constant, picking non-nan value work. i'm curious how fill e.g. mean/majority count.

using groupby along ffill , bfill

df[['lat', 'lng']]=df.groupby('place').ffill().bfill() 

df:

    place   lat lng 0   foo 1   4 1   foo 1   4 2   foo 1   4 3   foo 1   4 4   bar 5   7 5   bar 5   7 6   bar 5   7 7   bar 5   7 8   bar 5   7     

Comments