I'm interested in forward filling both single and multiple values in a column in pandas. With the following dataframe:
import pandas as pd
df = pd.DataFrame([[1, 2, 3], [4, None, None], [None, None, 9]])
df
0 1 2
0 1 2 3
1 4 NaN NaN
2 NaN NaN 9
Forward fill will produce:
df = pd.DataFrame([[1, 2, 3], [4, None, None], [None, None, 9]])
df.fillna(method='ffill')
df
0 1 2
0 1 2 3
1 4 2 3
2 4 2 9
However, I need an ffill-like method that will do this, or alternatively copy all above values if the values above follow one another:
df = pd.DataFrame([[1, 2, 3], [4, None, None], [None, 5, 9], [None,None,None])
df
0 1 2
0 1 2 3
1 4 NaN NaN
2 NaN 5 9
3 NaN NaN NaN
Resulting in:
df
0 1 2
0 1 2 3
1 4 2 3
2 1 5 9
3 4 5 9
Major edit: In my data the values will always be followed by NaNs in an unknown multiple of the length of values. Take df[0], for instance 1,4 would repeat for as long as there are NaNs. The only rule is that they will be a multiple of the length of the values (2)