根据索引过滤 Pandas DataFrame
在本文中,我们将了解如何根据索引过滤 Pandas Dataframe。我们可以在 filter() 的帮助下根据索引过滤 Dataframe。此方法用于根据指定索引中的标签对 Dataframe 的行或列进行子集。我们可以使用以下语法根据索引过滤 Dataframe。
Syntax: DataFrame.filter ( items=None, like=None, regex=None, axis=None )
Parameters:
- items : List of info axis to restrict to (must not all be present).
- like : Keep info axis where “arg in col == True”
- regex : Keep info axis with re.search(regex, col) == True
- axis : The axis to filter on. By default this is the info axis, ‘index’ for Series, ‘columns’ for DataFrame
Returns : This method is return same type of object as input object.
示例 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)
输出