Skip to content
Advertisement

How to repeat a value in a column based on another

I have the following dataframe:

JavaScript
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

output:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement