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

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.

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 3

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Posez-moi des questions sur ce sujet

Résumer ce chapitre

Afficher des exemples du monde réel

Awesome!

Completion rate improved to 3.33

bookAccessing Specific Rows

Glissez pour afficher le menu

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.

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 3
some-alt