'Pandas fillna by group [duplicate]

First and foremost, this sounds like a really stupid question, and I think the answer should be simple.

Yet... I'm stuck with this problem for weeks and I'm still not able to solve it.

What I need, is to just use something like fillna(method="ffill") in groups. Considering the DataFrame below:

import pandas as pd

df = pd.read_csv("something.csv"
>>> df
    group date        price
0   1     2021-12-01  .15
1   1     2022-01-15  NaN
2   1     2021-02-03  .35
3   2     2021-12-01  NaN
4   2     2021-12-15  2.5
5   2     2022-02-03  NaN
6   3     2021-11-15  3.25
6   3     2021-12-03  NaN
6   3     2022-01-06  NaN

The desired output is:

>>> df
    group date        price
0   1     2021-12-01  .15
1   1     2022-01-15  .15
2   1     2021-02-03  .35
3   2     2021-12-01  NaN
4   2     2021-12-15  2.5
5   2     2022-02-03  2.5
6   3     2021-11-15  3.25
6   3     2021-12-03  3.25
6   3     2022-01-06  3.25

What I have tried so far:

df["price"] = df.groupby(["group", "date"])["price"].ffill()
df["price"] = df.groupby(["group", "date"])["price"].fillna(method="ffill")
df = df.groupby(["group", "date"]).fillna(method="ffill")
df["price"] = df.groupby(["group", "date"])["price"].apply(lambda x :x.ffill())

And several similar answers I found, but none of them gave the expected result.



Sources

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

Source: Stack Overflow

Solution Source