Deleting a DataFrame row in Python Pandas based on column value

PandasServer Side ProgrammingProgramming

To delete a DataFrame row in Pandas based on column value, we can take the following Steps −

Steps

  • Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.

  • Print the input DataFrame.

  • Here, we will delete the row from the DataFrame that contains 0 in its Z-column, using df=df[df.z != 0]

  • Print the updated DataFrame, after deleting row based on column value.

Example

 Live Demo

import pandas as pd

df = pd.DataFrame(
   {
      "x": [5, 2, 1, 9],
      "y": [4, 1, 5, 10],
      "z": [4, 1, 5, 0]
   }
)
print "Input DataFrame is:\n", df
df = df[df.z != 0]
print "After deleting row based on column value:\n", df

Output

Input DataFrame is:
   x  y  z
0  5  4  4
1  2  1  1
2  1  5  5
3  9 10  0

After deleting row based on column value:
   x  y  z
0  5  4  4
1  2  1  1
2  1  5  5
raja
Published on 30-Aug-2021 09:44:43
Advertisements