I have a dataframe df
:
import pandas as pd
s = {'id': [243,243, 243, 243, 443,443,443, 332,334,332,332, 333],
'col':[1,1,1,1,1,1,1,2,2,2,2,2],
'st': [1,3,5,9,12, 18,23, 1,2,4,8,14],
'value':[2.4, 3.8, 3.7, 5.6, 1.2, 0.2, 2.1, 2.0, 2.5, 3.4, 1.2, 2.4]}
df = pd.DataFrame(s)
It looks like:
id col st value
0 243 1 1 2.4
1 243 1 3 3.8
2 243 1 5 3.7
3 243 1 9 5.6
4 443 1 12 1.2
5 443 1 18 0.2
6 443 1 23 2.1
7 332 2 1 2.0
8 334 2 2 2.5
9 332 2 4 3.4
10 332 2 8 1.2
11 333 2 14 2.4
The data have two groups col
1 and 2(in real data many groups). I want to include the missing records on the basis of the st
column. and the values must be kept as 0.
My output must look like
id col st value
243 1 1 2.4
0 1 2 0
243 1 3 3.8
0 1 4 0
243 1 5 3.7
and so on
332 2 1 2.0
334 2 2 2.5
0 2 3 0
332 2 4 3.4
0 2 5 0
0 2 6 0
0 2 7 0
332 2 8 1.2
How can I do this in pandas ?