- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Select rows from a Pandas DataFrame based on column values
To select rows from a DataFrame based on column values, we can take the following Steps −
Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.
Print the input DataFrame.
Use df.loc[df["x"]==2] to print the DataFrame when x==2.
Similarly, print the DataFrame when (x >= 2) and (x < 2).
Example
import pandas as pd df = pd.DataFrame( { "x": [5, 2, 1, 9], "y": [4, 1, 5, 10], "z": [4, 1, 5, 0] } ) print "Given DataFrame is:
", df print "When column x value == 2:
", df.loc[df["x"] == 2] print "When column x value >= 2:
", df.loc[df["x"] >= 2] print "When column x value < 2:
", df.loc[df["x"] < 2]
Output
Given DataFrame is: x y z 0 5 4 4 1 2 1 1 2 1 5 5 3 9 10 0 When column x value == 2: x y z 1 2 1 1 When column x value >= 2: x y z 0 5 4 4 1 2 1 1 3 9 10 0 When column x value < 2: x y z 2 1 5 5
- Related Articles
- Use a list of values to select rows from a Pandas DataFrame
- Python Pandas - Select a subset of rows from a dataframe
- Python - Filter Rows Based on Column Values with query function in Pandas?
- Python Pandas - How to select multiple rows from a DataFrame
- Select DataFrame rows between two index values in Python Pandas
- Python - How to select a column from a Pandas DataFrame
- Deleting a DataFrame row in Python Pandas based on column value
- Creating a Pandas dataframe column based on a given condition in Python
- Python Pandas - How to select rows from a DataFrame by integer location
- How to select rows based on range of values of a column in an R data frame?
- Python Pandas - How to select rows from a DataFrame by passing row label
- Python Pandas – How to select DataFrame rows on the basis of conditions
- Python - Select multiple columns from a Pandas dataframe
- Python Pandas - Filtering few rows from a DataFrame on the basis of sum
- How to delete a column from Pandas DataFrame

Advertisements