I have a dataframe df
import pandas as pd
s = {'id': [243,243, 243, 243, 443,443,443],
'st': [1,3,5,9,12, 18,23],
'value':[2.4, 3.8, 3.7, 5.6, 1.2, 0.2, 2.1]}
df = pd.DataFrame(s)
which looks like:
id st value
0 243 1 2.4
1 243 3 3.8
2 243 5 3.7
3 243 9 5.6
4 443 12 1.2
5 443 18 0.2
6 443 23 2.1
I need to fill in the missing rows based on the values in st
, the values in value
and id
column should be copied from the previous record. My output should look something like
id st value
243 1 2.4
243 2 2.4
243 3 3.8
243 4 3.8
243 5 3.7
243 6 3.7
243 7 3.7
243 8 3.7
243 9 5.6
243 10 5.6
243 11 5.6
443 12 1.2
443 13 1.2
and so on.
How can I do this in pandas dataframe ?
Here I am trying to fill the missing records rather than filling just the missing values.