'Front-fill Null values in a dataframe separated by group

I want to fill NAs using the most recent data and have it apply to groups.

This is my dataframe

pet    id     weight (lbs)
dog    1      30
dog    2      23
dog    3      NaN
cat    4      10
dog    5      NaN
cat    6      NaN
dog    7      39
cat    8      18
hippo  9      138

This is what I want the output to be

pet    id     weight (lbs)
dog    1      30
dog    2      23
dog    3      23
cat    4      10
dog    5      23
cat    6      10
dog    7      39
cat    8      18
hippo  9      138

This is the code for reproducing the dataframe-

df = pd.DataFrame({'pets':['dog', 'dog', 'dog', 'cat', 'dog', 'cat', 'dog', 'cat', 'hippo'],
                   'id':[1, 2, 3, 4, 5, 6, 7, 8, 8],
                   'Weight':[30, 23, np.nan, 10, np.nan, np.nan, 39, 10, 138]})

In other words, I want to fill in NaNs with the most recent non-null value grouped by pet and order by id. This is the code I tried to use: dataframe.sort_values(by = 'id').groupby('pet').fillna(method = 'ffill')



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source