w3resource

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

Pandas: DataFrame Exercise-29 with Solution

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.

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.