Course Content
Introduction to pandas [track]
Introduction to pandas [track]
Accessing 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 third0
,1
,2
(pay attention, such method doesn't include the last index).data[:3]
- returns rows from zero to third0
,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 datasetdata
for all columns.
# 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])
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.
Thanks for your feedback!