Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Navigating Data | Pandas Foundations
Introduction to Pandas with AI

bookNavigating Data

AI in Action

123456789101112
import 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
copy
Note
Note

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:

1234567891011
import 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"]])
copy

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:

123456789101112
import 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"]])
copy

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

12345678910
import 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"]])
copy

.loc[] includes the end label in slices.

DataFrame

1234567891011121314
import 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"]])
copy

The first argument selects rows; the second selects columns. You can pass labels, slices, or lists to either.

Note
Note

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

12345678910
import 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]])
copy

.iloc[] excludes the end position in slices.

DataFrame

1234567891011121314
import 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]])
copy

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?

question mark

What does df["Age"] return if df is a DataFrame?

Select the correct answer

question mark

Which statements about .loc[] are correct?

Select the correct answer

question mark

Which statements about .iloc[] are correct?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 4

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 10

bookNavigating Data

Sveip for å vise menyen

AI in Action

123456789101112
import 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
copy
Note
Note

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:

1234567891011
import 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"]])
copy

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:

123456789101112
import 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"]])
copy

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

12345678910
import 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"]])
copy

.loc[] includes the end label in slices.

DataFrame

1234567891011121314
import 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"]])
copy

The first argument selects rows; the second selects columns. You can pass labels, slices, or lists to either.

Note
Note

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

12345678910
import 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]])
copy

.iloc[] excludes the end position in slices.

DataFrame

1234567891011121314
import 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]])
copy

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?

question mark

What does df["Age"] return if df is a DataFrame?

Select the correct answer

question mark

Which statements about .loc[] are correct?

Select the correct answer

question mark

Which statements about .iloc[] are correct?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 4
some-alt