How to Filter Pandas DataFrame by index

Pandas-search

In this post, we are going to learn how to Filter Pandas DataFrame by index using the dataframe filter() method that includes a filter dataframe by a single index, filter dataframe by multiple indexes, filter by non-numeric index, filter row by condition using like operator.

1.Filter Pandas dataframe by single index using filter()


In this example, we have used dataframe filter() methods to filter a single index by passing the item argument and axis =0 to filter a single row. Let us understand with an example

Python Pandas Program to filter dataframe using filter()

import pandas as pd
 
  
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':[70,80,100],
    'Subj': ['Math', 'Math', 'Music']
}
  
 
dfobj = pd.DataFrame(Student_dict)
 
 
#filter dataframe by single index
 
df_one = dfobj.filter(items = [1], axis=0)
print(df_one)
 


Output

  Name  Marks  Subj
1  Rack     80  Math

2. Filter Pandas dataframe by mutiple indexes


In this Python program to filter dataframe multiple columns bypassing item [0,1] argument with axis =0 to select multiple rows by indexes.

Python Program filter dataframe by mutiple indexes

import pandas as pd
 
  
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':[70,80,100],
    'Subj': ['Math', 'Math', 'Music']
}
  
 
dfobj = pd.DataFrame(Student_dict)

#filter dataframe by mutiple indexes
 
df_muti = dfobj.filter(items = [0,1], axis=0)
 
print('\nfilter based on mutiple index:\n',df_muti)

Output

filter based on mutiple index:
    Name  Marks  Subj
0  Jack     70  Math
1  Rack     80  Math

3. Filter Pandas Dataframe Using non-numeric string index


In this Python program, We are filtering the dataframe by using a non-numeric strings index. We have string indexes ‘Row_1′,’Row_2′,’Row_3’ to filter rows by string index we have passed item = [] and with axis=0

Python Program to filter rows by non-numeric index

import pandas as pd
 
  
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':[70,80,100],
    'Subj': ['Math', 'Math', 'Music']
}
  
 
dfobj = pd.DataFrame(Student_dict,index =['Row_1','Row_2','Row_3'])
 
 
#single row
 
df_one = dfobj.filter(items = ['Row_2'], axis=0)
print(df_one)

Output

   Name  Marks  Subj
Row_2  Rack     80  Math

4. Filter Pandas dataframe index by condition like operator


Sometimes instead of index, we can use the like operator to filter multiple indexes by conditions. In this python program, we have used like =’ row’ string to filter all the rows that indexes contain ‘Row’ string index. Let us understand with the below example.

Python Program using like operator

import pandas as pd
 
  
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':[70,80,100],
    'Subj': ['Math', 'Math', 'Music']
}
  
 
dfobj = pd.DataFrame(Student_dict,index =['Row_1','Row_2','Row_3'])
 
 
 
 #filter dataframe by condition 
df_multi = dfobj.filter(like = 'Row', axis=0)
print(df_multi)

Output

  Name  Marks   Subj
Row_1  Jack     70   Math
Row_2  Rack     80   Math
Row_3   Max    100  Music

5. Regex to Filter Pandas DataFrame by column with regex


To filter dataframe by column we have used regex in filter() method by passing argument regex = ‘e$’ with axix=1. It will filter all columns of the dataframe.

Python Program to filter dataframe columns

import pandas as pd
 
  
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':[70,80,100],
    'Subj': ['Math', 'Math', 'Music']
}
  
 
dfobj = pd.DataFrame(Student_dict)

#filter dataframe column by using regex

df_col = dfobj.filter(regex='e$', axis=1)
print('\nfilter dataframe by column:\n',df_col)

Output

filter dataframe by column:
    Name
0  Jack
1  Rack
2   Max

6. Filter Pandas DataFrame by columname


In this python program, we have filtered the entire dataframe by column name in this we are looking for the column which name start with [N].To filter the multiple columns you can pass dfobj.filter(regex= ‘[N,S,M]’, axis=1).

Python Program to filter dataframe by columname

import pandas as pd
 
  
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':[70,80,100],
    'Subj': ['Math', 'Math', 'Music']
}
  
 
dfobj = pd.DataFrame(Student_dict)

#filter dataframe by specific columns
df_col = dfobj.filter(regex= '[N]', axis=1)
print('\nfilter dataframe by column:\n',df_col)

Output

filter dataframe by column:
    Name
0  Jack
1  Rack
2   Max

Summary

In this post, we have learned how to Filter Pandas DataFrame by index one or multiples by using the filter() method that includes using single or multiple indexes,non-numeric string index, or multiple indexes by conditions, filter dataframe column by regex, filter dataframe by column name with program example.