w3resource

Pandas: Delete DataFrame row(s) based on given column value


29. Delete Rows by Column Value

Write a Pandas program to delete DataFrame row(s) based on given column value.

Sample data:
Original DataFrame
col1 col2 col3
0 1 4 7
1 4 5 8
2 3 6 9
3 4 7 0
4 5 8 1
New DataFrame
col1 col2 col3
0 1 4 7
2 3 6 9
3 4 7 0
4 5 8 1

Sample Solution :

Python Code :

import pandas as pd
import numpy as np
d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
df = pd.DataFrame(data=d)
print("Original DataFrame")
print(df)
df = df[df.col2 != 5]
print("New DataFrame")
print(df)

Sample Output:

    Original DataFrame
   col1  col2  col3
0     1     4     7
1     4     5     8
2     3     6     9
3     4     7     0
4     5     8     1
New DataFrame
   col1  col2  col3
0     1     4     7
2     3     6     9
3     4     7     0
4     5     8     1              

Explanation:

The above code first creates a Pandas DataFrame df with columns col1, col2, and col3 using a dictionary ‘d’.

df = df[df.col2 != 5]: This line filters the rows of the DataFrame where the value in col2 is not equal to 5 using boolean indexing. Specifically, it creates a boolean mask df.col2 != 5, which returns True for all rows where the value in col2 is not 5, and False otherwise. This mask is then passed to the DataFrame to select only the rows where the mask is True, effectively filtering out the row where col2 is equal to 5.

Finally print() function prints the resulting DataFrame.


For more Practice: Solve these Related Problems:

  • Write a Pandas program to drop rows where a specified column value matches a given criterion and then reset the index.
  • Write a Pandas program to filter out rows based on a column value using boolean indexing and then display the remaining DataFrame.
  • Write a Pandas program to remove rows with a specific value in one column and then verify the deletion by counting rows.
  • Write a Pandas program to delete rows using the drop() method with a list of index labels determined by a column condition.

Python-Pandas Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to count city wise number of people from a given of data set (city, name of the person).
Next: Write a Pandas program to widen output display to see more columns.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.