Drop rows from the dataframe based on certain condition applied on a column

Drop rows from the dataframe based on certain condition applied on a column

You can use the Pandas library in Python to drop rows from a DataFrame based on a condition applied to a column. Here's how you can do it:

Let's assume you have the following DataFrame:

import pandas as pd data = { 'A': [1, 2, 3, 4, 5], 'B': [10, 20, 30, 40, 50] } df = pd.DataFrame(data) print(df) 

This will produce:

 A B 0 1 10 1 2 20 2 3 30 3 4 40 4 5 50 

Now, let's say you want to drop rows where the column 'A' has a value less than 3:

df = df[df['A'] >= 3] print(df) 

The output will be:

 A B 2 3 30 3 4 40 4 5 50 

Here's a breakdown of the code:

  • The condition df['A'] >= 3 generates a boolean mask where each row is marked as True if it meets the condition, and False otherwise.

  • By using this boolean mask to index the DataFrame (df[df['A'] >= 3]), you select only the rows where the condition evaluates to True.

The rows that do not meet the condition are effectively "dropped" from the resulting DataFrame.


More Tags

guzzle end-of-line next.js pyserial memoization namespaces ionic-native oc4j array-formulas kotlin

More Programming Guides

Other Guides

More Programming Examples