Suppose df is a pandas framework. I want to break it into two pieces of data according to a specific criterion. The best way I found for this is something like
df0, df1 = [v for _, v in df.groupby(df['class'] != 'special')]
In the above example, the criterion is the argument of the groupby method. The resulting df0 consists of sub-data, where the class field has the value 'special' , and df1 is basically a complement to df0 . (Unfortunately, with this design, the part of the data subfile consisting of elements that do not fulfill the criterion, which is not intuitive, is first returned.)
The above construction has the disadvantage that it is not particularly readable, of course, not as readable as, for example, some hypothetical splitby method like
df0, df1 = df.splitby(df['class'] == 'special')
Since splitting a data frame like this I often need to do, I suppose there might be a built-in function for this, or possibly an established idiom. If yes, let me know.
source share