基于索引过滤Pandas数据框架

基于索引过滤Pandas数据框架

在这篇文章中,我们将看到如何基于索引来过滤Pandas Dataframe。我们可以在filter()的帮助下基于索引过滤Dataframe。该方法用于根据指定索引中的标签对Dataframe的行或列进行子集。我们可以使用下面的语法来过滤基于索引的Dataframe。

语法: DataFrame.filter ( items=None, like=None, regex=None, axis=None )

参数 :

  • items :要限制的信息轴的列表(必须不全部存在)。
  • like:保持信息轴,其中 “arg in col == True”
  • regex : 保持信息轴与re.search(regex, col) == True
  • axis:要过滤的轴。默认情况下,这是信息轴,”索引 “用于系列,”列 “用于数据框架。

返回:该方法返回与输入对象相同的对象类型。

示例 1:

下面的程序是为了了解如何根据数值索引来过滤Dataframe。

# import pandas
import pandas as pd
 
# define data
data = {"Name": ["Mukul", "Suraj", "Rohit",
                 "Rahul", "Mohit", "Nishu",
                 "Rishi", "Manoj", "Mukesh",
                 "Rohan"],
        "Age": [22, 23, 25, 21, 27, 24, 26,
                23, 21, 27],
        "Qualification": ["BBA", "BCA", "BBA",
                          "BBA", "MBA", "BCA",
                          "MBA", "BBA", "BCA",
                          "MBA"]
        }
 
# define dataframe
df = pd.DataFrame(data, columns=['Name', 'Age', 'Qualification'])
 
# display original dataframe
print("\n Original Dataframe \n", df)
 
# filter 5 index value
df_1 = df.filter(items=[5], axis=0)
 
# display result
print("\n Display only 5 index value \n", df_1)
 
# filter only 5 and 8  index value
df_2 = df.filter(items=[5, 8], axis=0)
 
# display result
print("\n Display only 5 and 8 index value \n", df_2)

输出:

基于索引过滤Pandas数据框架

示例 2:

下面的程序是为了了解如何根据非数值索引来过滤Dataframe。

# import pandas
import pandas as pd
 
# define data
data = {"Name": ["Mukul", "Suraj", "Rohit",
                 "Rahul", "Mohit"],
        "Age": [22, 23, 25, 21, 27],
        "Qualification": ["BBA", "BCA", "BBA",
                          "BBA", "MBA"]
        }
 
# define dataframe
df = pd.DataFrame(data, columns=['Name', 'Age', 'Qualification'], index=[
                  'Person_A', 'Person_B', 'Person_C', 'Person_D', 'Person_E'])
 
# display original dataframe
print("\n Original Dataframe \n", df)
 
# filter Person_B index value
df_1 = df.filter(items=['Person_B'], axis=0)
 
# display result
print("\n Display only Person_B index value \n", df_1)
 
# filter only Person_B and Person_D index value
df_2 = df.filter(items=['Person_B', 'Person_D'], axis=0)
 
# display result
print("\n Display Person_B and Person_D index value \n", df_2)

输出:

基于索引过滤Pandas数据框架

示例 3:

下面的程序是为了了解如何过滤Dataframe中包含一个特定字符串的索引。

# import pandas
import pandas as pd
 
# define data
data = {"Name": ["Mukul", "Suraj", "Rohit",
                 "Rahul", "Mohit"],
        "Age": [22, 23, 25, 21, 27],
        "Qualification": ["BBA", "BCA", "BBA",
                          "BBA", "MBA"]
        }
 
# define dataframe
df = pd.DataFrame(data, columns=['Name', 'Age', 'Qualification'], index=[
                  'Person_A', 'Person_B', 'Person_AB', 'Person_c', 'Person_AC'])
 
# display original dataframe
print("\n Original Dataframe \n", df)
 
# filter index that contain Person_A string.
df_1 = df.filter(like='Person_A', axis=0)
 
# display result
print("\n display index that contain Person_A string \n", df_1)

输出

基于索引过滤Pandas数据框架

示例 4:

下面的程序是为了了解如何过滤Dataframe中包含特定字符的索引。

# import pandas
import pandas as pd
 
# define data
data = {"Name": ["Mukul", "Suraj", "Rohit",
                 "Rahul", "Mohit"],
        "Age": [22, 23, 25, 21, 27],
        "Qualification": ["BBA", "BCA", "BBA",
                          "BBA", "MBA"]
        }
 
# define dataframe
df = pd.DataFrame(data, columns=['Name', 'Age', 'Qualification'], index=[
                  'Person_A', 'Person_B', 'Person_AB', 'Person_C', 'Person_BC'])
 
# display original dataframe
print("\n Original Dataframe \n", df)
 
# filter index that contain an specific character.
df_1 = df.filter(like='B', axis=0)
 
# display result
print("\n display all indexes that contain Specific character \n", df_1)

输出

基于索引过滤Pandas数据框架

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程