Search code examples
pythonpandaspandas-groupbyfillna

How to fill na based on other value in the same group by Python


I want to fill NA in my dataset based on the value from the other row in the same group.

The data looks like this.

 group_id, start_time, end_time  
    1, NA, 20000
    1, 40000, 20000
    1, 30000, NA
    2, NA, 35000
    2, 45000, 22000
    2, 50000,21000
    2, 45000, NA

I want to get this result:

 group_id, start_time, end_time  
        1, 30000, 20000
        1, 40000, 20000
        1, 30000, 20000
        2, 45000, 35000
        2, 45000, 22000
        2, 50000,21000
        2, 45000, 35000

So the first and last value on each group are same on both start_time, and end_time .


Solution

  • You can do this using fillna, groupby, tranform and the first or last aggregation functions, as explained in this answer

    df['start_time'] = df['start_time'].fillna(df.groupby('group_id')['start_time'].transform('last'))
    df['end_time'] = df['end_time'].fillna(df.groupby('group_id')['end_time'].transform('first'))