Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Accessing Specific Rows | Accessing DataFrame Values
Introduction to pandas [track]
course content

Course Content

Introduction to pandas [track]

Introduction to pandas [track]

1. Basics
2. Reading and Exploring Data
3. Accessing DataFrame Values
4. Aggregate Functions

bookAccessing Specific Rows

Likewise you can access columns, you can access rows too. First, we consider how to get rows by their positions.

You should be familiar with list indexing. Let's remind that indexing in Python starts with 0, and right boundary in slicing is not included. Since single value within square brackets refers to column, to access rows we must use slicing, even for a single row.

Assume we have DataFrame with 1000 rows.

  • data[0:1] - returns only the first row (since the right boundary is not included).
  • data[0:3] - returns rows from zero to third 0, 1, 2 (pay attention, such method doesn't include the last index).
  • data[:3] - returns rows from zero to third 0, 1, 2 for all columns.
  • data[997:999] - returns rows from 997 to 999: 997, 998 for all columns (this is the last row).
  • data[997:] - returns rows from 997 to the last one: 997, 998, 999 for all columns.
  • data[:] - returns all rows of the dataset data for all columns.
123456789
# Importing library import pandas as pd # Reading csv file df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/67798cef-5e7c-4fbc-af7d-ae96b4443c0a/audi.csv') # Get the 10th row print(df[9:10]) # Get the 23rd - 25th rows print(df[22:25])
copy

Please note that the first row has an index of 0, that's why 23rd row has an index of 22. The right boundary, just like for lists, is not included while slicing.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 3
some-alt