﻿ Pandas: Delete DataFrame row(s) based on given column value - 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
```

Python Code Editor:

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

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

﻿

## Python: Tips of the Day

Dictionary comprehension:

```>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
```