How to Drop Rows in Python Pandas DataFrames [4 Examples]

Do you want to drop rows from a DataFrame? In this Python tutorial, I will explain how to drop rows in Python pandas dataframes with some illustrative examples.

To drop rows in Python Pandas DataFrames, we can use the drop() function with an index to remove specific rows, apply a condition for conditional removal, or utilize dropna() to exclude rows with missing values. For direct modification of the original DataFrame, the inplace=True parameter in the drop() method is used, allowing for efficient in-place row deletion.

Drop rows in Python Pandas DataFrames

The primary method to drop rows in Python Pandas DataFrames is the drop() function. The drop() function in Pandas is used to remove specified rows or columns from a DataFrame. We can choose to drop rows (axis=0) or columns (axis=1), and the operation can be made permanent by setting inplace=True.

The basic syntax of the drop() function is:

DataFrame.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')

Here,

  1. labels: Single label or list-like, indicating the index or columns to be dropped.
  2. axis: {0 or ‘index’, 1 or ‘columns’}, default 0. Whether to drop labels from the index (0 or ‘index’) or columns (1 or ‘columns’).
  3. index: Alternative to specifying axis (as axis=0).
  4. columns: Alternative to specifying axis (as axis=1).
  5. level: Used to specify the level in a MultiIndex.
  6. inplace: If True, performs operation inplace and returns None.
  7. errors: If ‘raise’, raises an error if the specified labels do not exist.
READ:  Compare two integers in Python Django

Let’s see some of the examples that how to drop rows in Python pandas dataframes:

1. Drop rows in Python by index

The df.drop() function in Python is primarily used for removing rows(or columns) from a DataFrame. When we specify a list of indices (like [1]), pandas will remove those rows.

This is how we can drop rows in Python pandas dataframes:

import pandas as pd

data1 = {'State': ['California', 'Texas', 'Florida', 'New York'], 'Capital': ['Sacramento', 'Austin', 'Tallahassee', 'Albany']}
df1 = pd.DataFrame(data1)
print("Original DataFrame:\n", df1)

df1_dropped = df1.drop([1])
print("\nDataFrame after dropping Texas:\n", df1_dropped)

Output:

Original DataFrame:
         State      Capital
0  California   Sacramento
1       Texas       Austin
2     Florida  Tallahassee
3    New York       Albany

DataFrame after dropping Texas:
         State      Capital
0  California   Sacramento
2     Florida  Tallahassee
3    New York       Albany

Following the execution of the code within the Pycharm editor, a screenshot of the outcome is displayed below.

How to drop rows in Python pandas dataframes

2. Pandas drop rows with condition

To drop rows in Python pandas dataframes, here we pass a boolean array to the DataFrame to filter the rows. The boolean array filters out rows from the DataFrame where the condition holds True.

For instance:

import pandas as pd

data2 = {'State': ['Alaska', 'Texas', 'California', 'Rhode Island'], 'Area': [663267, 268596, 163696, 1214]}
df2 = pd.DataFrame(data2)
print("Original DataFrame:\n", df2)

df2_filtered = df2[df2['Area'] > 100000]
print("\nDataFrame after filtering small states:\n", df2_filtered)

Output:

Original DataFrame:
           State    Area
0        Alaska  663267
1         Texas  268596
2    California  163696
3  Rhode Island    1214

DataFrame after filtering small states:
         State    Area
0      Alaska  663267
1       Texas  268596
2  California  163696

The screenshot provided below showcases the output generated after running the code in the PyCharm editor.

df drop row by condition in Python

3. Python drop row with missing values

This method is used to remove rows with missing values (NaNs). By default, dropna() removes any row that contains at least one missing value.

READ:  Python Tkinter notebook Widget

This is how we can drop rows in Python pandas dataframes using the dropna() function:

import pandas as pd

data3 = {'State': ['California', 'Texas', 'Florida', 'New York'], 'Capital': ['Sacramento', 'Austin', None, 'Albany']}
df3 = pd.DataFrame(data3)
print("DataFrame with a missing value:\n", df3)

df3_no_missing = df3.dropna()
print("\nDataFrame after dropping rows with missing values:\n", df3_no_missing)

Output:

DataFrame with a missing value:
         State     Capital
0  California  Sacramento
1       Texas      Austin
2     Florida        None
3    New York      Albany

DataFrame after dropping rows with missing values:
         State     Capital
0  California  Sacramento
1       Texas      Austin
3    New York      Albany

Below is a screenshot depicting the output, captured after the code was run in the PyCharm editor.

how to drop a row in python Pandas

4. Drop row Python Pandas using inplace parameter

The inplace=True parameter, allows the operation to modify the DataFrame directly, rather than returning a new DataFrame. If inplace is not set or set to False, the original DataFrame remains unchanged, and a new DataFrame is returned.

For example:

import pandas as pd

data4 = {'State': ['California', 'Texas', 'Florida', 'New York'], 'Capital': ['Sacramento', 'Austin', 'Tallahassee', 'Albany']}
df4 = pd.DataFrame(data4)
print("Original DataFrame:\n", df4)

df4.drop([2], inplace=True)
print("\nDataFrame after in-place dropping of Florida:\n", df4)

Output:

Original DataFrame:
         State      Capital
0  California   Sacramento
1       Texas       Austin
2     Florida  Tallahassee
3    New York       Albany

DataFrame after in-place dropping of Florida:
         State     Capital
0  California  Sacramento
1       Texas      Austin
3    New York      Albany

Following the execution of the code in PyCharm, the resulting output is captured in the screenshot displayed below.

delete row pandas in Python

Conclusion

Here, I have explained how to drop rows in Python Pandas DataFrames through four illustrative examples: using the drop() function with an index to remove specific rows, filtering rows out with conditions, eliminating rows containing missing values using dropna() function, and applying the inplace parameter for direct modification of the DataFrame.

READ:  Build Artificial Neural Network in Tensorflow

These methods collectively cover the versatile capabilities of pandas in row deletion.

You may also like to read: