- 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
Use a list of values to select rows from a Pandas DataFrame
To select the rows from a Pandas DataFrame based on input values, we can use the isin() method.
Steps
Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.
Print the input DataFrame.
Create a list of values for selection of rows.
Print the selected rows with the given values.
Next, print the rows that were not selected.
Example
import pandas as pd df = pd.DataFrame( { "x": [5, 2, 1, 9], "y": [4, 1, 5, 10], "z": [4, 1, 5, 0] } ) print "Input DataFrame:
", df values = [1, 2] print "Selected Rows:
", df[df['x'].isin(values)] print "Uselected Rows:
", df[~df['x'].isin(values)]
Output
Input DataFrame: x y z 0 5 4 4 1 2 1 1 2 1 5 5 3 9 10 0 Selected Rows: x y z 1 2 1 1 2 1 5 5 Unselected Rows: x y z 0 5 4 4 3 9 10 0
- Related Articles
- Select rows from a Pandas DataFrame based on column values
- Python Pandas - Select a subset of rows from a dataframe
- Python Pandas - How to select multiple rows from a DataFrame
- Python Pandas - How to select rows from a DataFrame by integer location
- Select DataFrame rows between two index values in Python Pandas
- Python Pandas - How to select rows from a DataFrame by passing row label
- Python - How to select a column from a Pandas DataFrame
- Python - Select multiple columns from a Pandas dataframe
- Python Pandas - Display specific number of rows from a DataFrame
- Python Pandas – How to select DataFrame rows on the basis of conditions
- Python - How to drop the null rows from a Pandas DataFrame
- Python - Remove duplicate values from a Pandas DataFrame
- Python - How to select a subset of a Pandas DataFrame
- Python - How to group DataFrame rows into list in Pandas?
- How to access a group of rows in a Pandas DataFrame?

Advertisements