I have the following dataframe:
import pandas as pd df = pd.DataFrame({ 'a': [1, 1, 2, 2], 'b': [None, 'w', None, 'z'] })
a | b |
---|---|
1 | None |
1 | ‘w’ |
2 | None |
2 | ‘z’ |
And I want to repeat the values that are not None
in column ‘b’, but based on the value in column ‘a’.
At the end I would have this dataframe:
a | b |
---|---|
1 | ‘w’ |
1 | ‘w’ |
2 | ‘z’ |
2 | ‘z’ |
Advertisement
Answer
The logic is not fully clear on how you would like to generalize, but you could bfill
/ffill
per group:
df['b'] = df.groupby('a')['b'].apply(lambda x: x.bfill().ffill())
output:
a b 0 1 w 1 1 w 2 2 z 3 2 z