Skip to content
Advertisement

Pandas create flag if any condition fails

I have a huge pandas dataframe (actually has 5M rows):

JavaScript

My requirement is to create a flag with value as 1 if any of the close dates cls1..5 is < the corresponding open date opn1..5

Example output: for id==2, cls5 < opn5

I want to avoid a loop and run it as fast as possible. There are ~5M rows in my data.

Perhaps np.where with a combination of any?

Advertisement

Answer

You can try using np.any with axis=0. First create the condition and then apply it on the relevant columns:

JavaScript

This will flag the rows with id=2 and id=5.

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