
- 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
How to access a group of rows in a Pandas DataFrame?
To access a group of rows in a Pandas DataFrame, we can use the loc() method. For example, if we use df.loc[2:5], then it will select all the rows from 2 to 5.
Steps
- Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.
- Print the input DataFrame, df.
- Use df.loc[2:5] to select the rows from 2 to 5.
- Print the DataFrame.
Example
import pandas as pd df = pd.DataFrame( { "x": [5, 2, 7, 0, 7, 0, 5, 2], "y": [4, 7, 5, 1, 5, 1, 4, 7], "z": [9, 3, 5, 1, 5, 1, 9, 3] } ) print "Input DataFrame is:\n", df df = df.loc[2:5] print "New DataFrame:\n", df
Output
Input DataFrame is: x y z 0 5 4 9 1 2 7 3 2 7 5 5 3 0 1 1 4 7 5 5 5 0 1 1 6 5 4 9 7 2 7 3 New DataFrame: x y z 2 7 5 5 3 0 1 1 4 7 5 5 5 0 1 1
- Related Questions & Answers
- Python - How to group DataFrame rows into list in Pandas?
- Python Pandas - How to append rows to a DataFrame
- How to iterate over rows in a DataFrame in Pandas?
- How to plot certain rows of a Pandas dataframe using Matplotlib?
- Python Pandas - Select a subset of rows from a dataframe
- Python - Compute last of group values in a Pandas DataFrame
- Python - Compute first of group values in a Pandas DataFrame
- Python - Summing all the rows of a Pandas Dataframe
- Python - Sum only specific rows of a Pandas Dataframe
- Python Pandas - How to select multiple rows from a DataFrame
- Delete the first three rows of a DataFrame in Pandas
- Python - Ranking Rows of Pandas DataFrame
- Use a list of values to select rows from a Pandas DataFrame
- Python - How to Group Pandas DataFrame by Month?
- Python - How to Group Pandas DataFrame by Days?
Advertisements