

- 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
Select DataFrame rows between two index values in Python Pandas
We can slice a Pandas DataFrame to select rows between two index values. Let's take an example and see how it's done.
Steps
- Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.
- Print the input DataFrame, df.
- Initialize a variable for lower limit of the index.
- Initialize another variable for upper limit of the index.
- Use df[index_lower_limit: index_upper_limit] to print the DataFrame in range index.
Example
import pandas as pd df = pd.DataFrame( { "x": [5, 2, 7, 0], "y": [4, 7, 5, 1], "z": [9, 3, 5, 1] } ) print "Input DataFrame is:\n", df index_lower_limit = 1 index_upper_limit = 3 print("DataFrame between two index values:\n", df[index_lower_limit: index_upper_limit])
Output
Input DataFrame is: x y z 0 5 4 9 1 2 7 3 2 7 5 5 3 0 1 1 DataFrame between two index values: x y z 1 2 7 3 2 7 5 5
- Related Questions & Answers
- Select rows from a Pandas DataFrame based on column values
- Python Pandas - How to select multiple rows from a DataFrame
- Python Pandas - Select a subset of rows from a dataframe
- Use a list of values to select rows from a Pandas DataFrame
- Python Pandas – Filter DataFrame between two dates
- Python - Ranking Rows of Pandas DataFrame
- Python Pandas - How to select rows from a DataFrame by integer location
- 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 Pandas - Finding the uncommon rows between two DataFrames
- Python – Merge two Pandas DataFrame
- Python - Drop specific rows from multiindex Pandas Dataframe
- Python - Select multiple columns from a Pandas dataframe
- Write a program in Python to select any random odd index rows in a given DataFrame
- How to display Pandas Dataframe in Python without Index?
Advertisements