Skip to content
Advertisement

pandas – idiomatic way to stash rows filtered out of dataframe

What is the idiomatic way to split a dataframe into two by a condition? Or put another way, to filter a dataframe by a condition but keep the filtered out rows in a new dataframe?

I was thinking I could filter the original dataframe for the inverse of the ‘filtered out by condition’ rows using the tilde , like this:

JavaScript
JavaScript

Advertisement

Answer

You can use df.loc + df.index.difference with nonvowels.index to achieve what you’re looking for (and yes, this is idiomatic):

JavaScript

Output:

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