forward fill specific column after groupby and reindex

707 views Asked by At

I would like to fill one specific column, the "id" column with the "id" in that group, and forward fill the other columns with 0 after applying a groupwise reindex.

my current version with filling all missing values with 0 looks like this:

def reindex_by_date(df):
    bd = pd.bdate_range('2000-12-18', '2020-12-31')
    return df.reindex(bd)
test.groupby('id').apply(reindex_by_date).reset_index(0, drop=True)

but I cant make it work to fill the "id" with id"s within that group

1

There are 1 answers

0
daniel On

I found a solution. Posting so maybe it will help someone else:

def reindex_by_date(df):
    df = df.reindex(pd.bdate_range('2000-12-18', '2020-12-31'))
    df['id']=df['id'].ffill().bfill()
    df['weight'] = df['weight'].fillna(0)
    df['return'] = df['return'].fillna(0)
    return (df)

test_new= test.groupby('id').apply(reindex_by_date).rename_axis(('id','date')).drop('id',1).reset_index()