📌  相关文章
📜  根据索引过滤 Pandas DataFrame

📅  最后修改于: 2022-05-13 01:55:51.663000             🧑  作者: Mango

根据索引过滤 Pandas DataFrame

在本文中,我们将了解如何根据索引过滤 Pandas Dataframe。我们可以在 filter() 的帮助下根据索引过滤 Dataframe。此方法用于根据指定索引中的标签对 Dataframe 的行或列进行子集。我们可以使用以下语法根据索引过滤 Dataframe。

示例 1:

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

Python
# 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)


Python
# 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)


Python
# 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)


Python
# 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)


输出:

示例 2:

下面的程序是要知道如何根据非数值索引过滤Dataframe。

Python

# 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)

输出:

示例 3:

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

Python

# 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)

输出

示例 4:

下面的程序是要知道如何为包含特定字符的索引过滤 Dataframe。

Python

# 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)

输出