Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Filtering and Updating Values | Pandas Foundations
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Introduction to Pandas with AI

bookFiltering and Updating Values

AI in Action

1234567891011
import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 25, 21], "Grade":[85, 62, 90, 68] }) students.loc[students["Grade"] < 70, "Grade"] += 5 top_students = students[students["Grade"] >= 70] print(top_students)
copy

Filtering Data

You can filter a DataFrame by creating boolean conditions - logical expressions that evaluate to either True or False for each row. You can then place that condition inside any access method ([], .loc[], and .iloc[]) to keep only the rows where the result is True.

1234567891011121314
import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 25, 21], "Grade":[85, 62, 90, 68] }) # Students with grade above 70 passed = students[students["Grade"] > 70] print(passed) # Students younger than 25 and with grade above 80 young_high = students[(students["Grade"] > 80) & (students["Age"] < 25)] print(young_high)
copy

Use logical operators like & (and), | (or), and ~ (not) to build complex conditions.

Updating Values

You can update single cells, whole columns, or conditional selections. The safest and clearest way to do this is with .loc[]:

1234567891011121314
import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 25, 21], "Grade":[85, 62, 90, 68] }) # Simple update students.loc[1, "Name"] = "Bobby" # Conditional update students.loc[students["Grade"] < 70, "Grade"] = 70 print(students)
copy

As long as you can select the desired part of a DataFrame, you can use other methods like .iloc[] or []. But .loc[] is recommended due to its flexibility.

Avoiding Chained Assignments

Chained assignment happens when you use two sets of brackets in sequence. It might modify only a temporary copy, not the original DataFrame:

1234567891011121314
import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 25, 21], "Grade":[85, 62, 90, 68] }) # Chain assignment students[students["Grade"] < 70]["Grade"] = 70 print(students) # Correct assignment students.loc[students["Grade"] < 70, "Grade"] = 70 print(students)
copy
Note
Note

When executing the code above, pandas will display a warning message. If you encounter them in your own code, don't ignore them - they often signal that something might not work as expected.

1. Which lines correctly filter students with a grade greater than 85?

2. How would you safely change Bob's grade to 90?

3. Why would df[df["Grade"] > 105]["Name"] = 70 raise a warning?

question mark

Which lines correctly filter students with a grade greater than 85?

Select the correct answer

question mark

How would you safely change Bob's grade to 90?

Select the correct answer

question mark

Why would df[df["Grade"] > 105]["Name"] = 70 raise a warning?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 6

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

bookFiltering and Updating Values

Sveip for å vise menyen

AI in Action

1234567891011
import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 25, 21], "Grade":[85, 62, 90, 68] }) students.loc[students["Grade"] < 70, "Grade"] += 5 top_students = students[students["Grade"] >= 70] print(top_students)
copy

Filtering Data

You can filter a DataFrame by creating boolean conditions - logical expressions that evaluate to either True or False for each row. You can then place that condition inside any access method ([], .loc[], and .iloc[]) to keep only the rows where the result is True.

1234567891011121314
import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 25, 21], "Grade":[85, 62, 90, 68] }) # Students with grade above 70 passed = students[students["Grade"] > 70] print(passed) # Students younger than 25 and with grade above 80 young_high = students[(students["Grade"] > 80) & (students["Age"] < 25)] print(young_high)
copy

Use logical operators like & (and), | (or), and ~ (not) to build complex conditions.

Updating Values

You can update single cells, whole columns, or conditional selections. The safest and clearest way to do this is with .loc[]:

1234567891011121314
import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 25, 21], "Grade":[85, 62, 90, 68] }) # Simple update students.loc[1, "Name"] = "Bobby" # Conditional update students.loc[students["Grade"] < 70, "Grade"] = 70 print(students)
copy

As long as you can select the desired part of a DataFrame, you can use other methods like .iloc[] or []. But .loc[] is recommended due to its flexibility.

Avoiding Chained Assignments

Chained assignment happens when you use two sets of brackets in sequence. It might modify only a temporary copy, not the original DataFrame:

1234567891011121314
import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 25, 21], "Grade":[85, 62, 90, 68] }) # Chain assignment students[students["Grade"] < 70]["Grade"] = 70 print(students) # Correct assignment students.loc[students["Grade"] < 70, "Grade"] = 70 print(students)
copy
Note
Note

When executing the code above, pandas will display a warning message. If you encounter them in your own code, don't ignore them - they often signal that something might not work as expected.

1. Which lines correctly filter students with a grade greater than 85?

2. How would you safely change Bob's grade to 90?

3. Why would df[df["Grade"] > 105]["Name"] = 70 raise a warning?

question mark

Which lines correctly filter students with a grade greater than 85?

Select the correct answer

question mark

How would you safely change Bob's grade to 90?

Select the correct answer

question mark

Why would df[df["Grade"] > 105]["Name"] = 70 raise a warning?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 6
some-alt