Skip to content
Advertisement

How to replicate same values based on the index value of other column in python

I have a dataframe like below and I want to add another column that is replicated untill certain condition is met.

JavaScript

Now I want to add another column which contains additional information about the dataframe. For instance, I want to replicate Yes untill id is B and No when it is below B and Yes from C to D and from from D to E Maybe.

The output I am expecting is as follows:

JavaScript

How can I achieve this in python?

Advertisement

Answer

You can use np.select to return results based on conditions. Since you were talking more about positional conditions I used df.index:

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