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 in Pandas based on values in multiple columns
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' : [ "apandey@gmail.com" , "pankaj@gmail.com" , "sumishra23@gmail.com" , "cgeeku@yahoo.com" , "anuj24@gmail.com" , "amanver@yahoo.com" , "madhav1998@gmail.com" , "rroy7@gmail.com" , "sagarwal36@gmail.com" ], '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: