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?
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Can you explain how boolean conditions work in pandas?
What are some common mistakes to avoid when updating DataFrames?
Can you show more examples of filtering and updating DataFrames?
Génial!
Completion taux amélioré à 5.26
Filtering and Updating Values
Glissez pour afficher le menu
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?
Merci pour vos commentaires !