python - Fill in missing pandas data with previous non-missing value, grouped by key -


i dealing pandas dataframes this:

   id    x 0   1   10 1   1   20 2   2  100 3   2  200 4   1  nan 5   2  nan 6   1  300 7   1  nan 

i replace each nan 'x' previous non-nan 'x' row same 'id' value:

   id    x 0   1   10 1   1   20 2   2  100 3   2  200 4   1   20 5   2  200 6   1  300 7   1  300 

is there slick way without manually looping on rows?

you perform groupby/forward-fill operation on each group:

import numpy np import pandas pd  df = pd.dataframe({'id': [1,1,2,2,1,2,1,1], 'x':[10,20,100,200,np.nan,np.nan,300,np.nan]}) df['x'] = df.groupby(['id'])['x'].ffill() print(df) 

yields

   id      x 0   1   10.0 1   1   20.0 2   2  100.0 3   2  200.0 4   1   20.0 5   2  200.0 6   1  300.0 7   1  300.0 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -