I have the following dataframe:
JavaScript
x
7
1
import pandas as pd
2
3
df = pd.DataFrame({
4
'a': [1, 1, 2, 2],
5
'b': [None, 'w', None, 'z']
6
})
7
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:
JavaScript
1
2
1
df['b'] = df.groupby('a')['b'].apply(lambda x: x.bfill().ffill())
2
output:
JavaScript
1
6
1
a b
2
0 1 w
3
1 1 w
4
2 2 z
5
3 2 z
6