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?
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Can you explain the difference between .loc[] and .iloc[] in more detail?
How do I select multiple rows or columns at once using these methods?
What happens if I use a label that doesn't exist in the Series or DataFrame?
Fantastisk!
Completion rate forbedret til 5.26
Navigating Data
Stryg for at vise menuen
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?
Tak for dine kommentarer!