Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data preprocessing
  • Data Manipulation
  • Data Analysis using Pandas
  • EDA
  • Pandas Exercise
  • Pandas AI
  • Numpy
  • Matplotlib
  • Plotly
  • Data Analysis
  • Machine Learning
  • Data science
Open In App
Next Article:
How to select multiple columns in a pandas dataframe
Next article icon

Split dataframe in Pandas based on values in multiple columns

Last Updated : 31 Jul, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we are going to see how to divide a dataframe by various methods and based on various parameters using Python. To divide a dataframe into two or more separate dataframes based on the values present in the column we first create a data frame.

Creating a DataFrame for demonestration

Python3
# importing pandas as pd import pandas as pd   # dictionary of lists dict = {'First_Name': ["Aparna", "Pankaj", "Sudhir",                         "Geeku", "Anuj", "Aman",                        "Madhav", "Raj", "Shruti"],         'Last_Name': ["Pandey", "Gupta", "Mishra",                        "Chopra", "Mishra", "Verma",                        "Sen", "Roy", "Agarwal"],         'Email_ID': ["[email protected]", "[email protected]",                      "[email protected]", "[email protected]",                      "[email protected]", "[email protected]",                      "[email protected]", "[email protected]",                      "[email protected]"],         'Degree': ["MBA", "BCA", "M.Tech", "MBA", "B.Sc",                    "B.Tech", "B.Tech", "MBA", "M.Tech"],         'Score': [90, 40, 75, 98, 94, 90, 80, 90, 95]}  # creating dataframe df = pd.DataFrame(dict)  print(df) 

Output:

Split dataframe based on values By Boolean Indexing

We can create multiple dataframes from a given dataframe based on a certain column value by using the boolean indexing method and by mentioning the required criteria.

Example 1: Creating a dataframe for the students with Score >= 80

Python3
# creating a new dataframe by applying the required  # conditions in []  df1 = df[df['Score'] >= 80]  print(df1) 

Output:

Example 2: Creating a dataframe for the students with Last_Name as Mishra

Python3
# Creating on the basis of Last_Name dfname = df[df['Last_Name'] == 'Mishra']  print(dfname) 

Output:

We can do the same for other columns as well by putting the appropriate condition

Split dataframe based on values Boolean Indexing with mask variable

We create a mask variable for the condition of the column in the previous method

Example 1: To get dataframe of students with Degree as MBA

Python3
# creating the mask variable with appropriate # condition mask_var = df['Degree'] =='MBA'  # creating a dataframe df1_mask = df[mask_var]  print(df1_mask) 

Output :

Example 2: To get a dataframe for the rest of the students

To get the rest of the values in a dataframe we can simply invert the mask variable by adding a ~(tilde) after it.

Python3
# creating dataframe with inverted mask variable df2_mask = df[~mask_var]  print(df2_mask) 

Output :

Split dataframe based on values Using groupby() function

Using groupby() we can group the rows using a specific column value and then display it as a separate dataframe.

Example 1: Group all Students according to their Degree and display as required

Python3
# Creating an object using groupby grouped = df.groupby('Degree')  # the return type of the object 'grouped' is  # pandas.core.groupby.generic.DataFrameGroupBy.  # Creating a dataframe from the object using get_group(). # dataframe of students with Degree as MBA. df_grouped = grouped.get_group('MBA')  print(df_grouped) 

Output: dataframe of students with Degree as MBA

Example 2: Group all Students according to their Score and display as required

Python3
# Creating another object using groupby grouped2 = df.groupby('Score')  # the return type of the object 'grouped2' is  # pandas.core.groupby.generic.DataFrameGroupBy.  # Creating a dataframe from the object  # using get_group() dataframe of students # with Score = 90 df_grouped2 = grouped2.get_group(90)  print(df_grouped2) 

Output: dataframe of students with Score = 90.


Next Article
How to select multiple columns in a pandas dataframe

R

riser24
Improve
Article Tags :
  • Python
  • Python-pandas
  • Python pandas-dataFrame
  • pandas-dataframe-program
Practice Tags :
  • python

Similar Reads

  • Sort Rows or Columns in Pandas Dataframe Based on Values
    Sorting is a fundamental operation when working with data in Pandas. Whether you need to arrange rows in ascending or descending order or reorder columns based on values, Pandas provides powerful functions to make sorting easy and efficient. In this article, we'll explore different ways to sort rows
    4 min read
  • Add multiple columns to dataframe in Pandas
    In Pandas, we have the freedom to add columns in the data frame whenever needed. There are multiple ways to add columns to pandas dataframe.  Add multiple columns to a DataFrame using Lists[GFGTABS] Python3 # importing pandas library import pandas as pd # creating and initializing a nested list stud
    3 min read
  • How to select multiple columns in a pandas dataframe
    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. In this article, we will discuss all the different ways of selecting multiple columns
    5 min read
  • Split Pandas Dataframe by column value
    Sometimes in order to analyze the Dataframe more accurately, we need to split it into 2 or more parts. The Pandas provide the feature to split Dataframe according to column index, row index, and column values, etc.  Let' see how to Split Pandas Dataframe by column value in Python?  Now, let's create
    3 min read
  • Unnest (Explode) Multiple List Columns In A Pandas Dataframe
    An open-source manipulation tool that is used for handling data is known as Pandas. Have you ever encountered a dataset that has columns with data as a list? In such cases, there is a necessity to split that column into various columns, as Pandas cannot handle such data. In this article, we will dis
    6 min read
  • Split a text column into two columns in Pandas DataFrame
    Let's see how to split a text column into two columns in Pandas DataFrame. Method #1 : Using Series.str.split() functions. Split Name column into two different columns. By default splitting is done on the basis of single space by str.split() function. # import Pandas as pd import pandas as pd # crea
    3 min read
  • Split a String into columns using regex in pandas DataFrame
    Given some mixed data containing multiple values as a string, let's see how can we divide the strings using regex and make multiple columns in Pandas DataFrame. Method #1: In this method we will use re.search(pattern, string, flags=0). Here pattern refers to the pattern that we want to search. It ta
    3 min read
  • Filter Pandas Dataframe by Column Value
    Filtering a Pandas DataFrame by column values is a common and essential task in data analysis. It allows to extract specific rows based on conditions applied to one or more columns, making it easier to work with relevant subsets of data. Let's start with a quick example to illustrate the concept: [G
    7 min read
  • How to drop one or multiple columns in Pandas DataFrame
    Let's learn how to drop one or more columns in Pandas DataFrame for data manipulation. Drop Columns Using df.drop() MethodLet's consider an example of the dataset (data) with three columns 'A', 'B', and 'C'. Now, to drop a single column, use the drop() method with the column’s name. [GFGTABS] Python
    4 min read
  • Split Pandas Dataframe by Column Index
    Pandas support two data structures for storing data the series (single column) and dataframe where values are stored in a 2D table (rows and columns).  To index  a  dataframe using the index we need to make use of dataframe.iloc() method which takes  Syntax: pandas.DataFrame.iloc[] Parameters:Index
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences