Pandas how to fill sequence of rows with the previous value in dataframe

Alexandru Pavel :

Suppose i have this datarame

df =pd.DataFrame([[1, 1, 0, 3], [1, 1, 1, 4], [1, 1, 3, 6], [2, 1, 0, 0], [2, 1, 3, 6]],
             columns=["id","code","date","count"])

Output:

   id  code  date  count
0   1     1     0      3
1   1     1     1      4
2   1     1     3      6
3   2     1     0      0
4   2     1     3      6

I want to fill the missing date number (this one is between 0 and 3) with the previous count based on id and code. Intended output:

   id  code  date  count
0   1     1     0      3
1   1     1     1      4
2   1     1     2      4
3   1     1     3      6
4   2     1     0      0
5   2     1     1      0
6   2     1     2      0
7   2     1     3      6
Quang Hoang :

In your case, a combination of pivot and stack:

(df.pivot_table(index=['id','code'],
               columns='date',
               values='count')
   .reindex(np.arange(4), axis=1)
   .ffill(1)
   .stack()
   .reset_index(name='count')
)

Output:

   id  code  date  count
0   1     1     0    3.0
1   1     1     1    4.0
2   1     1     2    4.0
3   1     1     3    6.0
4   2     1     0    0.0
5   2     1     1    0.0
6   2     1     2    0.0
7   2     1     3    6.0

Update: if you have more than one count columns, it's a bit more tricky:

(df.pivot_table(index=['id','code'],
               columns='date')
   .stack(level=0)
   .reindex(np.arange(4), axis=1)
   .ffill(1)
   .unstack(level=-1)
   .stack(level=0)
   .reset_index()
)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=350916&siteId=1