Home>

Hello everyone!

There is the following DataFrame:

Title ID Date
Point A 1 01.11
Point A 02.11
Point A 2 02.11
Point B 02.11
Point B 3 01.11
Point B 4 02.11

It is necessary to fill in the gaps in the ID column, but the ID should be taken exactly according to the last date at each point, i.e. If you look at an example, then point A should have ID 2, and point B -4. The question is, how can this be done using pandas?

  • Answer # 1

    Try this:

    df ["ID"]= df ["ID"]. fillna (df.groupby ("Name") ["ID"]. transform ("last"))
    

    result:

    In [273]: df
    Out [273]:
      Name ID Date
    0 Point A 1.0 1.11
    1 Point A 2.0 2.11
    2 Point A 2.0 2.11
    3 Point B 4.0 2.11
    4 Point B 3.0 1.11
    5 Point B 4.0 2.11