Navigating Data
AI in Action
123456789101112import pandas as pd ages = pd.Series([24, 30, 27, 22], index=["Alice", "Bob", "Carol", "Dan"]) students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 27, 22], "Grade": [85, 62, 90, 78] }) print(ages["Carol"]) # From Series print(students.loc[2, "Age"]) # From DataFrame
All three access styles in pandas - [], .loc[], and .iloc[] - support:
- selecting by a single value;
- selecting by a range (slice);
- selecting by a list.
Accessing Data with []
Series
With Series, [] can be used to select both by label and by position. Positional selection still works, but is not recommended:
1234567891011import pandas as pd ages = pd.Series([24, 30, 27, 22], index=["Alice", "Bob", "Carol", "Dan"]) # Single print(ages["Bob"]) # Range print(ages["Bob":"Carol"]) print(ages[1:3]) # List print(ages[["Alice", "Dan"]])
Label-based slices in Series include both endpoints. Positional slices follow normal Python slicing rules (end excluded).
DataFrame
For DataFrames, [] is used only for column selection:
123456789101112import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 27, 22], "Grade": [85, 62, 90, 78] }) # Single print(students["Age"]) # List print(students[["Name", "Grade"]])
df["column"] returns a Series, while df[["column"]] returns a DataFrame.
Accessing Data with .loc[]
.loc[] selects by label - for both rows and columns.
Series
12345678910import pandas as pd ages = pd.Series([24, 30, 27, 22], index=["Alice", "Bob", "Carol", "Dan"]) # Single print(ages.loc["Alice"]) # Range print(ages.loc["Bob":"Dan"]) # List print(ages.loc[["Alice", "Dan"]])
.loc[] includes the end label in slices.
DataFrame
1234567891011121314import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 27, 22], "Grade": [85, 62, 90, 78] }) # Single print(students.loc[0, "Grade"]) # Range print(students.loc[0:2, "Name":"Age"]) # List print(students.loc[[0, 2], ["Name", "Grade"]])
The first argument selects rows; the second selects columns. You can pass labels, slices, or lists to either.
Even though the range in 0:2 from the example is written with numbers, it is still interpreted as a labeled range, not a positional one.
Accessing Data with .iloc[]
.iloc[] selects by position - for both rows and columns.
Series
12345678910import pandas as pd ages = pd.Series([24, 30, 27, 22], index=["Alice", "Bob", "Carol", "Dan"]) # Single print(ages.iloc[1]) # Range print(ages.iloc[1:3]) # List print(ages.iloc[[0, 2, 3]])
.iloc[] excludes the end position in slices.
DataFrame
1234567891011121314import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 27, 22], "Grade": [85, 62, 90, 78] }) # Single print(students.iloc[1, 2]) # Range print(students.iloc[0:2, 0:2]) # List print(students.iloc[[0, 1], [0, 2]])
Just like with .loc[], the first argument selects rows; the second selects columns. You can pass positions, slices, or lists to either.
1. What does df["Age"] return if df is a DataFrame?
2. Which statements about .loc[] are correct?
3. Which statements about .iloc[] are correct?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 10
Navigating Data
Swipe to show menu
AI in Action
123456789101112import pandas as pd ages = pd.Series([24, 30, 27, 22], index=["Alice", "Bob", "Carol", "Dan"]) students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 27, 22], "Grade": [85, 62, 90, 78] }) print(ages["Carol"]) # From Series print(students.loc[2, "Age"]) # From DataFrame
All three access styles in pandas - [], .loc[], and .iloc[] - support:
- selecting by a single value;
- selecting by a range (slice);
- selecting by a list.
Accessing Data with []
Series
With Series, [] can be used to select both by label and by position. Positional selection still works, but is not recommended:
1234567891011import pandas as pd ages = pd.Series([24, 30, 27, 22], index=["Alice", "Bob", "Carol", "Dan"]) # Single print(ages["Bob"]) # Range print(ages["Bob":"Carol"]) print(ages[1:3]) # List print(ages[["Alice", "Dan"]])
Label-based slices in Series include both endpoints. Positional slices follow normal Python slicing rules (end excluded).
DataFrame
For DataFrames, [] is used only for column selection:
123456789101112import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 27, 22], "Grade": [85, 62, 90, 78] }) # Single print(students["Age"]) # List print(students[["Name", "Grade"]])
df["column"] returns a Series, while df[["column"]] returns a DataFrame.
Accessing Data with .loc[]
.loc[] selects by label - for both rows and columns.
Series
12345678910import pandas as pd ages = pd.Series([24, 30, 27, 22], index=["Alice", "Bob", "Carol", "Dan"]) # Single print(ages.loc["Alice"]) # Range print(ages.loc["Bob":"Dan"]) # List print(ages.loc[["Alice", "Dan"]])
.loc[] includes the end label in slices.
DataFrame
1234567891011121314import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 27, 22], "Grade": [85, 62, 90, 78] }) # Single print(students.loc[0, "Grade"]) # Range print(students.loc[0:2, "Name":"Age"]) # List print(students.loc[[0, 2], ["Name", "Grade"]])
The first argument selects rows; the second selects columns. You can pass labels, slices, or lists to either.
Even though the range in 0:2 from the example is written with numbers, it is still interpreted as a labeled range, not a positional one.
Accessing Data with .iloc[]
.iloc[] selects by position - for both rows and columns.
Series
12345678910import pandas as pd ages = pd.Series([24, 30, 27, 22], index=["Alice", "Bob", "Carol", "Dan"]) # Single print(ages.iloc[1]) # Range print(ages.iloc[1:3]) # List print(ages.iloc[[0, 2, 3]])
.iloc[] excludes the end position in slices.
DataFrame
1234567891011121314import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 27, 22], "Grade": [85, 62, 90, 78] }) # Single print(students.iloc[1, 2]) # Range print(students.iloc[0:2, 0:2]) # List print(students.iloc[[0, 1], [0, 2]])
Just like with .loc[], the first argument selects rows; the second selects columns. You can pass positions, slices, or lists to either.
1. What does df["Age"] return if df is a DataFrame?
2. Which statements about .loc[] are correct?
3. Which statements about .iloc[] are correct?
Thanks for your feedback!