Pandas – Drop List of Rows From DataFrame

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:January 23, 2023
Spread the love

By using pandas.DataFrame.drop() method you can remove/delete/drop the list of rows from pandas, all you need to provide is a list of rows indexes or labels as a param to this method. By default drop() method removes the rows and returns a copy of the updated DataFrame instead of replacing the existing referring DataFrame. If you wanted to remove from the DataFrame in place use inplace=True param. By default, the value for inplace property is False meaning not to update the existing DataFrame.

By using the same drop() method, you can also remove columns in pandas DataFrame by using axis=1

1. Create a Sample DataFrame

Let’s create a pandas DataFrame to explain how to remove list of rows with examples, my DataFrame contains the column names Courses, Fee, Duration, and Discount.


# Create a Sample DataFrame
import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Hadoop","Python","pandas","Oracle","Java"],
    'Fee' :[20000,25000,26000,22000,24000,21000,22000],
    'Duration':['30day','40days','35days', '40days','60days','50days','55days'],
    'Discount':[1000,2300,1500,1200,2500,2100,2000]
               }
df = pd.DataFrame(technologies)
print(df)

Yields below output.


# Output:
   Courses    Fee Duration  Discount
0    Spark  20000    30day      1000
1  PySpark  25000   40days      2300
2   Hadoop  26000   35days      1500
3   Python  22000   40days      1200
4   pandas  24000   60days      2500
5   Oracle  21000   50days      2100
6     Java  22000   55days      2000

2. pandas Drop List of Rows by Index

pandas.DataFrame.drop() function takes a list of indexes/labels that you wanted to delete, this function returns a copy of DataFrame without modifying the reference DataFrame.


# Drop by List of Index position
df1 = df.drop([2,4])
print(df1)

Yields below output.


# Output:
Table with the dropped rows
   Courses    Fee Duration  Discount
0    Spark  20000    30day      1000
1  PySpark  25000   40days      2300
3   Python  22000   40days      1200
5   Oracle  21000   50days      2100
6     Java  22000   55days      2000

If you wanted to create a new index after removing the list of rows.


# Reset index
df = pd.DataFrame(technologies)
df1 = df.drop([2,4]).reset_index()
print(df1)

4. Drop a List of Rows From DataFrame inplace

If you notice by default drop() method returns the copy of the DataFrame after removing rows, but if you wanted to update the existing DataFrame, use inplace=True parameter. when you used inplace=True param, DataFrame returns None instead of DataFrame. For E.x df.drop([3,5], inplace=True) drops the specified list of rows from df object.


# Drop a List of Rows From DataFrame inplace
df = pd.DataFrame(technologies)
df.drop([2,4],inplace=True)
print(df)

Yields same output as above.

5. Removing Range of Rows From One to Five

You can use python list slicing to delete a list of rows from 1 to 5 for example, df.drop(df.index[:5],inplace= True) function remove one to five rows.


# Removing Range of Rows From One to Five
df = pd.DataFrame(technologies)
df.drop(df.index[:5],inplace=True)
print(df)

Yields below output.


# Output:
Removing Range of Rows from 0 to 5
  Courses    Fee Duration  Discount
5  Oracle  21000   50days      2100
6    Java  22000   55days      2000

6. Drop List of Rows by Index Position in DataFrame

In case if you have index labels on DataFrame, you can also remove the list of rows by position. For example, below removes 3rd and 5th records from DataFrame. Note index starts from zero.


# Drop List of Rows by Index Position in DataFrame
indexes=['r1','r2','r3','r4','r5','r6','r7']
df = pd.DataFrame(technologies,index=indexes)
df.drop(df.index[[2,4]],inplace=True)
print(df)

Yields below output.


# Output:
    Courses    Fee Duration  Discount
r1    Spark  20000    30day      1000
r2  PySpark  25000   40days      2300
r4   Python  22000   40days      1200
r6   Oracle  21000   50days      2100
r7     Java  22000   55days      2000

You can also try removing the rows by applying pandas filter.

Conclusion

In this article you have learned how to remove a list of DataFrame rows in pandas using the drop() function, also learned how to remove rows by a list of indexes and labels.

Happy Learning !!

References

Leave a Reply

You are currently viewing Pandas – Drop List of Rows From DataFrame