Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Write a program in Python to print the 'A' grade students' names from a DataFrame
Sometimes we need to filter DataFrame records based on specific column values. In this example, we'll filter a student DataFrame to show only students with 'A' grades and display their names.
Sample DataFrame
Let's start by creating a DataFrame with student information ?
import pandas as pd
data = [[1,'stud1','A'],[2,'stud2','B'],[3,'stud3','C'],[4,'stud4','A'],[5,'stud5','A']]
df = pd.DataFrame(data, columns=('Id','Name','Grade'))
print("DataFrame is\n", df)
DataFrame is Id Name Grade 0 1 stud1 A 1 2 stud2 B 2 3 stud3 C 3 4 stud4 A 4 5 stud5 A
Filtering 'A' Grade Students
Use boolean indexing to filter rows where the Grade column equals 'A' ?
import pandas as pd
data = [[1,'stud1','A'],[2,'stud2','B'],[3,'stud3','C'],[4,'stud4','A'],[5,'stud5','A']]
df = pd.DataFrame(data, columns=('Id','Name','Grade'))
# Filter students with 'A' grade
result = df[df['Grade']=='A']
print("Students with 'A' grade:")
print(result['Name'])
Students with 'A' grade: 0 stud1 3 stud4 4 stud5 Name: Name, dtype: object
Alternative Methods
Using query() Method
The query() method provides a more readable syntax for filtering ?
import pandas as pd
data = [[1,'stud1','A'],[2,'stud2','B'],[3,'stud3','C'],[4,'stud4','A'],[5,'stud5','A']]
df = pd.DataFrame(data, columns=('Id','Name','Grade'))
# Using query method
result = df.query("Grade == 'A'")
print("Names of 'A' grade students:")
print(result['Name'].tolist())
Names of 'A' grade students: ['stud1', 'stud4', 'stud5']
Using loc[] Method
The loc[] indexer allows filtering and column selection in one step ?
import pandas as pd
data = [[1,'stud1','A'],[2,'stud2','B'],[3,'stud3','C'],[4,'stud4','A'],[5,'stud5','A']]
df = pd.DataFrame(data, columns=('Id','Name','Grade'))
# Using loc with boolean indexing
names = df.loc[df['Grade'] == 'A', 'Name']
print("'A' grade students:")
print(names.values)
'A' grade students: ['stud1' 'stud4' 'stud5']
How It Works
The boolean indexing approach df['Grade']=='A' creates a boolean Series where each element is True if the Grade equals 'A', and False otherwise. This boolean Series is then used to filter the DataFrame, returning only rows where the condition is True.
Conclusion
Use boolean indexing df[df['Grade']=='A']['Name'] for simple filtering. For more complex conditions, consider query() method or loc[] for better readability and performance.
