Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9306477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:10:56+00:00 2026-06-19T00:10:56+00:00

Suppose that df is a pandas dataframe. I want to split it into two

  • 0

Suppose that df is a pandas dataframe. I want to split it into two dataframes according to some criterion. The best way I’ve found for doing this is something like

df0, df1 = [v for _, v in df.groupby(df['class'] != 'special')]

In the above example, the criterion is the argument to the groupby method. The resulting df0 consists of the sub-dataframe where the class field has value 'special', and df1 is basically the complement of df0. (Unfortunately, with this construct, the sub-dataframe consisting of the items that fail the criterion are returned first, which is not intuitive.)

The above construct has the drawback that it is not particularly readable, certainly not as readable as, for instance, some hypothetical splitby method like

df0, df1 = df.splitby(df['class'] == 'special')

Since splitting a dataframe like this is something I often need to do, I figure that there may be a built-in function, or maybe an established idiom, for doing this. If so, please let me know.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T00:10:58+00:00Added an answer on June 19, 2026 at 12:10 am

    I think the most readable way is to do this is:

    m = df['class'] != 'special'
    a, b = df[m], df[~m]
    

    I haven’t come across a special method for this…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a Pandas dataframe df has columns a,b,c,d...z . And I want
Suppose that you have two huge files (several GB) that you want to concatenate
Suppose that I have already proved some theorem in coq, and later I want
Suppose that you have a bunch of projects in your Eclipse workspace. Some are
Suppose that the Activity Tree has two activities (Activity 1 and Activity 2). Activity
Suppose that you have a 2D grid of cells, some of which are filled
Suppose that I have 2 files: File1.txt 10;30;15;40;12;14;15 23;32;10;50;12;54;60 File2.txt 2;4;5;6;7;8;9 3;6;7;8;9;0;7 I want
Suppose that whe have some object, derived from these class instances class A {
Suppose that I have an (unsorted in any way) array: [ 12 64 35
Suppose I have the example TimeSeries below: ts = pandas.TimeSeries({'a':[1,2,3,4,5], 'b':[6,7,8,9,10]}) The best method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.