please click here for more wordpress cource
To remove rows in a Pandas DataFrame based on a condition, you can use the drop
method. Here’s an example:
Suppose you have a DataFrame df
with a column named ‘age’ and you want to remove all rows where the age is less than 18.
import pandas as pd
# Create a sample DataFrame
data = {'name': ['Alice', 'Bob', 'Charlie', 'Dave'],
'age': [25, 17, 21, 16]}
df = pd.DataFrame(data)
# Remove all rows where age is less than 18
df = df.drop(df[df.age < 18].index)
# Print the modified DataFrame
print(df)
Output:
name age
0 Alice 25
2 Charlie 21
In the above code, the drop
method is used to drop rows where the age is less than 18. The condition df.age < 18
returns a Boolean Series that indicates which rows satisfy the condition. The drop
method is then called on the DataFrame with the index of these rows to remove them. The index
attribute of the Boolean Series returns the index of the rows where the condition is True.