Bird
Raised Fist0
Data Analysis Pythondata~5 mins

Selecting rows (loc, iloc) in Data Analysis Python

Choose your learning style10 modes available
Introduction

We use loc and iloc to pick specific rows from a table of data. This helps us look at or work with just the parts we want.

You want to see data for a specific date or label in a table.
You need to grab rows by their position number, like the first 5 rows.
You want to filter rows based on row names or indexes.
You want to select rows between two labels or positions.
You want to quickly check or analyze a small part of a big dataset.
Syntax
Data Analysis Python
df.loc[row_label]
df.loc[start_label:end_label]
df.iloc[row_position]
df.iloc[start_pos:end_pos]

loc uses the row labels or names to select rows.

iloc uses the row numbers (starting at 0) to select rows.

Examples
Selects the row with label '2023-01-01'. Useful if your rows are dates or named.
Data Analysis Python
df.loc['2023-01-01']
Selects rows from '2023-01-01' up to '2023-01-03' inclusive.
Data Analysis Python
df.loc['2023-01-01':'2023-01-03']
Selects the very first row by position number 0.
Data Analysis Python
df.iloc[0]
Selects the first three rows by position numbers 0, 1, and 2.
Data Analysis Python
df.iloc[0:3]
Sample Program

This code creates a table with sales data for 5 days. It shows how to pick rows by date labels with loc and by position with iloc.

Data Analysis Python
import pandas as pd

# Create a simple DataFrame with dates as index
data = {'Sales': [100, 150, 200, 130, 170]}
dates = pd.date_range('2023-01-01', periods=5)
df = pd.DataFrame(data, index=dates)

print('Original DataFrame:')
print(df)

# Select row by label using loc
print('\nRow for 2023-01-03 using loc:')
print(df.loc['2023-01-03'])

# Select rows by label range using loc
print('\nRows from 2023-01-02 to 2023-01-04 using loc:')
print(df.loc['2023-01-02':'2023-01-04'])

# Select row by position using iloc
print('\nFirst row using iloc:')
print(df.iloc[0])

# Select first three rows using iloc
print('\nFirst three rows using iloc:')
print(df.iloc[0:3])
OutputSuccess
Important Notes

loc includes the last label in a slice, but iloc does not include the last position.

If your DataFrame index is not labeled, loc may not work as expected; use iloc for position-based selection.

Both loc and iloc can select multiple rows and columns by adding a comma, e.g., df.loc['2023-01-01':'2023-01-03', 'Sales'].

Summary

loc selects rows by their labels or names.

iloc selects rows by their position numbers, starting at zero.

Use these to easily pick the exact rows you want from your data.

Practice

(1/5)
1. Which of the following statements correctly describes the difference between loc and iloc in pandas?
easy
A. loc selects rows by label, iloc selects rows by position.
B. loc selects columns by position, iloc selects rows by label.
C. loc selects rows by position, iloc selects columns by label.
D. loc and iloc both select rows by position.

Solution

  1. Step 1: Understand loc usage

    loc selects rows using the row labels or names, not their numeric position.
  2. Step 2: Understand iloc usage

    iloc selects rows based on their integer position, starting at zero.
  3. Final Answer:

    loc selects rows by label, iloc selects rows by position. -> Option A
  4. Quick Check:

    loc = label, iloc = position [OK]
Hint: Remember: loc = label, iloc = integer position [OK]
Common Mistakes:
  • Confusing label with position
  • Using iloc with labels
  • Assuming loc uses integer positions
2. Which of the following is the correct syntax to select the row with label '2023-01-01' from a DataFrame df using loc?
easy
A. df.loc[2023-01-01]
B. df.iloc[2023-01-01]
C. df.iloc['2023-01-01']
D. df.loc['2023-01-01']

Solution

  1. Step 1: Identify correct method for label selection

    To select by label, use loc with the label as a string if it is a string label.
  2. Step 2: Check syntax for string label

    The label '2023-01-01' must be in quotes inside loc brackets.
  3. Final Answer:

    df.loc['2023-01-01'] -> Option D
  4. Quick Check:

    Use quotes for string labels with loc [OK]
Hint: Use quotes around string labels inside loc brackets [OK]
Common Mistakes:
  • Omitting quotes around string labels
  • Using iloc with labels
  • Using loc without brackets
3. Given the DataFrame df below:
import pandas as pd
data = {'Name': ['Anna', 'Bob', 'Cara', 'Dave'], 'Age': [23, 35, 45, 29]}
df = pd.DataFrame(data, index=['a', 'b', 'c', 'd'])

What is the output of df.loc['b']?
medium
A. Name Bob Age 35 Name: 1, dtype: object
B. Name Cara Age 45 Name: c, dtype: object
C. Name Bob Age 35 Name: b, dtype: object
D. Error: KeyError

Solution

  1. Step 1: Understand DataFrame index labels

    The DataFrame has index labels 'a', 'b', 'c', 'd'. The row with label 'b' corresponds to Bob, age 35.
  2. Step 2: Use loc to select row by label

    df.loc['b'] returns the row with label 'b' as a Series with Name and Age values.
  3. Final Answer:

    Name Bob Age 35 Name: b, dtype: object -> Option C
  4. Quick Check:

    loc selects row 'b' with Bob's data [OK]
Hint: loc uses index labels exactly as given [OK]
Common Mistakes:
  • Using iloc instead of loc
  • Confusing label 'b' with position 1
  • Expecting a DataFrame instead of Series
4. Consider this code snippet:
import pandas as pd
data = {'Value': [10, 20, 30]}
df = pd.DataFrame(data, index=[1, 2, 3])
row = df.loc[2]

What is wrong with this code if the intention was to select the second row by position?
medium
A. It selects the row with label 2, not the second row by position.
B. It raises a KeyError because 2 is not a label.
C. It correctly selects the second row by position.
D. It raises a TypeError because loc needs a string.

Solution

  1. Step 1: Check DataFrame index labels

    The DataFrame index labels are [1, 2, 3]. Using loc[2] selects the row with label 2, not the second row by position.
  2. Step 2: Understand difference between label and position

    The second row by position is at index 1 (zero-based), so iloc[1] should be used to select by position.
  3. Final Answer:

    It selects the row with label 2, not the second row by position. -> Option A
  4. Quick Check:

    loc uses labels, iloc uses positions [OK]
Hint: Use iloc for position, loc for label [OK]
Common Mistakes:
  • Using loc to select by position
  • Confusing index labels with positions
  • Expecting loc to be zero-based
5. You have a DataFrame df with a non-numeric index:
import pandas as pd
data = {'Score': [88, 92, 95, 70]}
labels = ['a', 'b', 'c', 'd']
df = pd.DataFrame(data, index=labels)

How would you select the first two rows by position and the rows labeled 'b' and 'c' by label, then combine these selections into one DataFrame?
hard
A. df.loc[:2] + df.loc[['b', 'c']]
B. pd.concat([df.iloc[:2], df.loc[['b', 'c']]])
C. df.iloc[['a', 'b']] + df.loc[1:2]
D. df.loc[['a', 'b']] + df.iloc[1:2]

Solution

  1. Step 1: Select first two rows by position

    Use df.iloc[:2] to select rows at positions 0 and 1.
  2. Step 2: Select rows labeled 'b' and 'c'

    Use df.loc[['b', 'c']] to select rows by their labels.
  3. Step 3: Combine selections into one DataFrame

    Use pd.concat to join the two selections vertically.
  4. Final Answer:

    pd.concat([df.iloc[:2], df.loc[['b', 'c']]]) -> Option B
  5. Quick Check:

    Combine iloc and loc selections with concat [OK]
Hint: Use pd.concat to join iloc and loc selections [OK]
Common Mistakes:
  • Trying to add DataFrames with + operator
  • Mixing labels and positions incorrectly
  • Using loc with positions or iloc with labels
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard