
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 Articles
- 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 - How to select rows from a DataFrame by integer location
- Python Pandas – How to select DataFrame rows on the basis of conditions
- Python Pandas – Filter DataFrame between two dates
- Python - Ranking Rows of Pandas DataFrame
- Python Pandas - How to select rows from a DataFrame by passing row label
- Write a program in Python to select any random odd index rows in a given DataFrame
- Python – Merge two Pandas DataFrame
- How to display Pandas Dataframe in Python without Index?
- Python - Drop specific rows from multiindex Pandas Dataframe
- Python - Select multiple columns from a Pandas dataframe
- Python Pandas - Finding the uncommon rows between two DataFrames

Advertisements