Filtering and Updating Values
AI in Action
1234567891011import 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)
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.
1234567891011121314import 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)
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[]:
1234567891011121314import 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)
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:
1234567891011121314import 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)
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?
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Fantastisk!
Completion rate forbedret til 5.26
Filtering and Updating Values
Sveip for å vise menyen
AI in Action
1234567891011import 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)
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.
1234567891011121314import 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)
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[]:
1234567891011121314import 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)
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:
1234567891011121314import 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)
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?
Takk for tilbakemeldingene dine!