Sjoseph
Sjoseph

Reputation: 873

Pandas dataframe column forward fill from first non-zero value

I am looking to forward fill specific dataframe columns from first non-zero value and I further want to do this for each group.

df = pd.DataFrame(np.array([[1, 0, 0], [1, 5, 1], [1, 8, 0],[2, 4, 0],[2, 8, 1],[2, 81, 0]]),
                   columns=['ID', 'b', 'c'])

The result I want is:

df2 = pd.DataFrame(np.array([[1, 0, 0], [1, 5, 1], [1, 8, 1],[2, 4, 0],[2, 8, 1],[2, 81, 1]]),
                   columns=['ID', 'b', 'c'])

Attempt:

df2 = df.groupby('ID',as_index = False)['c'].apply(lambda x: x.replace(to_replace=0, method='ffill'))

The problem is the original dataframe is not returned. Any help with this would be much appreciated!

Upvotes: 1

Views: 1035

Answers (2)

Nk03
Nk03

Reputation: 14949

Try this -

df['c'] = df.groupby('ID')['c'].apply(lambda x: x.replace(to_replace=0, method='ffill')) #print df after this.

Upvotes: 2

Anurag Dabas
Anurag Dabas

Reputation: 24314

Use .values attribute:

df['c']=df.groupby('ID',as_index = False)['c'].apply(lambda x: x.replace(to_replace=0, method='ffill')).values

Now if you print df you will get your desired output:

    ID  b   c
0   1   0   0
1   1   5   1
2   1   8   1
3   2   4   0
4   2   8   1
5   2   81  1

Upvotes: 2

Related Questions