python - Deleting DataFrame row in Pandas based on column value

Python - Deleting DataFrame row in Pandas based on column value

To delete rows from a Pandas DataFrame based on a column value, you can use the drop method along with boolean indexing. Here's a step-by-step guide to accomplish this:

  1. Import Pandas: Make sure you have Pandas installed and import it.
  2. Create a DataFrame: If you don't already have a DataFrame, create one for demonstration.
  3. Delete Rows Based on Column Value: Use boolean indexing to filter out rows you want to delete.

Here's an example to demonstrate these steps:

Step 1: Import Pandas

First, make sure to import the pandas library.

import pandas as pd 

Step 2: Create a DataFrame

Let's create a sample DataFrame.

data = { 'Name': ['John', 'Anna', 'Peter', 'Linda', 'James'], 'Age': [28, 24, 35, 32, 30], 'City': ['New York', 'Paris', 'Berlin', 'London', 'Tokyo'] } df = pd.DataFrame(data) print("Original DataFrame:") print(df) 

Step 3: Delete Rows Based on Column Value

Suppose you want to delete all rows where the City is 'Paris'.

# Delete rows where City is 'Paris' df = df[df['City'] != 'Paris'] print("\nDataFrame after deleting rows where City is 'Paris':") print(df) 

Putting it all together

Here's the complete code:

import pandas as pd # Step 1: Create a DataFrame data = { 'Name': ['John', 'Anna', 'Peter', 'Linda', 'James'], 'Age': [28, 24, 35, 32, 30], 'City': ['New York', 'Paris', 'Berlin', 'London', 'Tokyo'] } df = pd.DataFrame(data) print("Original DataFrame:") print(df) # Step 2: Delete rows where City is 'Paris' df = df[df['City'] != 'Paris'] print("\nDataFrame after deleting rows where City is 'Paris':") print(df) 

This will output:

Original DataFrame: Name Age City 0 John 28 New York 1 Anna 24 Paris 2 Peter 35 Berlin 3 Linda 32 London 4 James 30 Tokyo DataFrame after deleting rows where City is 'Paris': Name Age City 0 John 28 New York 2 Peter 35 Berlin 3 Linda 32 London 4 James 30 Tokyo 

In this example, the row where the City is 'Paris' has been removed from the DataFrame. You can adjust the condition inside the boolean indexing to delete rows based on different column values or different conditions.

Examples

  1. How to delete rows from a DataFrame based on a single column value in Pandas?

    • Description: This query involves deleting rows from a DataFrame where a specific column matches a given value.
    • Code:
      import pandas as pd # Create a sample DataFrame df = pd.DataFrame({ 'A': [1, 2, 3, 4], 'B': ['a', 'b', 'c', 'd'] }) # Delete rows where column 'A' is equal to 2 df = df[df['A'] != 2] print(df) 
  2. How to remove rows in a Pandas DataFrame where the column value is in a list?

    • Description: This query demonstrates how to delete rows based on whether the column value is present in a list of values.
    • Code:
      import pandas as pd # Create a sample DataFrame df = pd.DataFrame({ 'A': [1, 2, 3, 4], 'B': ['a', 'b', 'c', 'd'] }) # List of values to remove values_to_remove = [2, 3] # Delete rows where column 'A' is in the list df = df[~df['A'].isin(values_to_remove)] print(df) 
  3. How to drop rows from a DataFrame based on multiple column values in Pandas?

    • Description: This query shows how to delete rows where multiple columns match specific values.
    • Code:
      import pandas as pd # Create a sample DataFrame df = pd.DataFrame({ 'A': [1, 2, 3, 4], 'B': ['a', 'b', 'c', 'd'] }) # Delete rows where column 'A' is 1 and column 'B' is 'a' df = df[~((df['A'] == 1) & (df['B'] == 'a'))] print(df) 
  4. How to delete rows in Pandas DataFrame based on column value condition?

    • Description: This query involves deleting rows based on a condition applied to a column.
    • Code:
      import pandas as pd # Create a sample DataFrame df = pd.DataFrame({ 'A': [1, 2, 3, 4], 'B': ['a', 'b', 'c', 'd'] }) # Delete rows where column 'A' is greater than 2 df = df[df['A'] <= 2] print(df) 
  5. How to remove rows from DataFrame based on string value in Pandas?

    • Description: This query demonstrates deleting rows where a column has a specific string value.
    • Code:
      import pandas as pd # Create a sample DataFrame df = pd.DataFrame({ 'A': [1, 2, 3, 4], 'B': ['a', 'b', 'c', 'd'] }) # Delete rows where column 'B' is 'b' df = df[df['B'] != 'b'] print(df) 
  6. How to drop rows in a Pandas DataFrame using a boolean mask?

    • Description: This query shows how to use a boolean mask to delete rows based on column values.
    • Code:
      import pandas as pd # Create a sample DataFrame df = pd.DataFrame({ 'A': [1, 2, 3, 4], 'B': ['a', 'b', 'c', 'd'] }) # Create a boolean mask mask = df['A'] > 2 # Delete rows using the boolean mask df = df[~mask] print(df) 
  7. How to filter and delete DataFrame rows based on column value using query() in Pandas?

    • Description: This query uses the query() method to filter out rows based on column values.
    • Code:
      import pandas as pd # Create a sample DataFrame df = pd.DataFrame({ 'A': [1, 2, 3, 4], 'B': ['a', 'b', 'c', 'd'] }) # Delete rows where column 'A' is 3 using query df = df.query('A != 3') print(df) 
  8. How to delete rows from DataFrame based on NaN values in a specific column in Pandas?

    • Description: This query involves deleting rows where a specific column contains NaN values.
    • Code:
      import pandas as pd import numpy as np # Create a sample DataFrame df = pd.DataFrame({ 'A': [1, 2, np.nan, 4], 'B': ['a', 'b', 'c', 'd'] }) # Delete rows where column 'A' has NaN values df = df.dropna(subset=['A']) print(df) 
  9. How to delete rows in Pandas DataFrame based on the index of a column value?

    • Description: This query demonstrates deleting rows based on the index of a specific column value.
    • Code:
      import pandas as pd # Create a sample DataFrame df = pd.DataFrame({ 'A': [1, 2, 3, 4], 'B': ['a', 'b', 'c', 'd'] }) # Get the index of rows where column 'A' is 3 index_to_drop = df[df['A'] == 3].index # Delete rows by index df = df.drop(index_to_drop) print(df) 
  10. How to delete rows in a DataFrame based on a condition in a different column in Pandas?

    • Description: This query involves deleting rows based on a condition applied to a different column.
    • Code:
      import pandas as pd # Create a sample DataFrame df = pd.DataFrame({ 'A': [1, 2, 3, 4], 'B': [10, 20, 30, 40] }) # Delete rows where column 'B' is less than 30 df = df[df['B'] >= 30] print(df) 

More Tags

v-navigation-drawer q# google-cloud-messaging session-state nested-forms dreamweaver nav mpdf distcp node-sass

More Programming Questions

More Fitness Calculators

More Pregnancy Calculators

More Stoichiometry Calculators

More Internet Calculators