
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a program in Python to print the ‘A’ grade students’ names from a DataFrame
Input −
Assume, you have DataFrame, Id Name Grade 0 1 stud1 A 1 2 stud2 B 2 3 stud3 C 3 4 stud4 A 4 5 stud5 A
Output −
And the result for ‘A’ grade students name,
0 stud1 3 stud4 4 stud5
Solution
To solve this, we will follow the below approaches.
Define a DataFrame
Compare the value to the DataFrame
df[df['Grade']=='A']
Store the result in another DataFrame and fetch Name.
Example
Let us see the following implementation to get a better understanding.
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) print("find the A grade students name\n") result = df[df['Grade']=='A'] print(result['Name'])
Output
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 find the A grade students name 0 stud1 3 stud4 4 stud5 Name: Name, dtype: object
- Related Questions & Answers
- Write a program to print ‘Tutorials Point’ without using a semicolon in C
- Python Program to print the pattern ‘G’
- Write a program in Python to store the city and state names that start with ‘k’ in a given DataFrame into a new CSV file
- Validate input: replace all ‘a’ with ‘@’ and ‘i’ with ‘!’JavaScript
- Python program to extract ‘k’ bits from a given position?
- How to delete a column of a dataframe using the ‘pop’ function in Python?
- Python program to replace first ‘K’ elements by ‘N’
- Write a program in C++ to count the Number of substrings that starts with ‘1’ and ends with ‘1’
- Write a program in Python to filter the elements in a series which contains a string start and endswith ‘a’
- Find all the names beginning with the letter 'a' or ‘b’ or ‘c’ using MySQL query?
- Python Pandas - Read data from a CSV file and print the ‘product’ column value that matches ‘Car’ for the first ten rows
- Write a C program to print ‘ABCD’ repeatedly without using loop, recursion and any control structure
- Java program to extract ‘k’ bits from a given position
- K-th digit in ‘a’ raised to power ‘b’ in C++
- Python Program to Replace all Occurrences of ‘a’ with $ in a String
Advertisements