Home > Back-end >  extract the remaining data from a dataset after filtering some data in Python
extract the remaining data from a dataset after filtering some data in Python

Time:03-10

Suppose I am having dataframe like this:

Length    Width    Height
 100       150      130
 120       140      150 
 200       170      120
 250       190      180
 270       200      195 

Now I want to filter the data from three columns in a way like Length between (100-200) and width between (130-170) and Height between (120-150), if I extract the data like this, it will give me the data frame like

Length    Width   Height
 100       150      130
 120       140      150 
 200       170      120

and the remaining data from the dataframe is like

   Length    Width   Height
     250       190      180
     270       200      195 

Now, I want to see both filtered data and the remaining data from the data set in a separate variables. How to get that in python?

CodePudding user response:

IIUC, use a boolean mask combining multiple between with &, and index difference:

mask = (df['Length'].between(100,200)
      & df['Width'].between(130,170)
      & df['Height'].between(120,150)
       )
df1 = df[mask]
df2 = df.loc[df.index.difference(df1.index)]
# or
# df2 = df[~mask]

output:

# df1
   Length  Width  Height
0     100    150     130
1     120    140     150
2     200    170     120

# df2
   Length  Width  Height
3     250    190     180
4     270    200     195
  • Related