• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:12 mins read
You are currently viewing Pandas Drop Rows by Index

How to perform Drop Rows by Index in Pandas DataFrame? By using the Pandas drop function we can drop/delete the row or list of rows by index labels or position. Using the drop function we can also drop multiple columns by index.

In this article, I will explain how to drop rows by index labels or position using the drop() function and using different approaches with examples.

  • drop() method is used to drop rows or columns from DataFrame.
  • Use the axis param to specify what axis you would like to remove. By default axis = 0 meaning to remove rows. Use axis=1 or columns param to remove columns.
  • Use inplace=True to remove row/column inplace meaning on existing DataFrame without creating a copy.

1. Quick Examples of Drop Rows by Index

If you are in a hurry, below are some quick examples of how to drop rows of DataFrame using an index.


# Below are some quick examples

# Example 1: Use DataFrame.iloc[] to drop row 
df1 = df.iloc[1:]

# Example 2: Drop rows from DataFrame using index
df1 = df.drop(index=df.iloc[3].name)

# Example 3: Drop the row using index labels
df1 = df.drop('r2')

# Example 4: Drop multiple rows using index labels
df1 = df.drop(['r1', 'r4', 'r5'])

# Example 5: Drop rows using index position
df1 = df.drop([df.index[1], df.index[2]])

# Example 6:  Set inplace = True & Drop multiple rows
df.drop(['r1', 'r4', 'r5'], inplace = True)

# Example 7: Use DataFrame.iloc[] to drop row 
df1 = df.iloc[1:]

# Example 8: Drop rows from DataFrame using index
df1 = df.drop(index=df.iloc[3].name)

Let’s create DataFrame using data from the Python dictionary and run the above examples to get the first row of DataFrame.


# Import pandas library
# Create DataFrame
import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas","Hadoop"],
    'Fee' :[20000,25000,22000,24000,30000],
    'Duration':['30day','40days','35days','60days','55days'],
    'Discount':[1000,2300,2500,2000,3000]
              }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame(technologies, index  = index_labels)
print("DataFrame:\n", df)

Yields below output.

pandas drop row index

2. Pandas Drop Row by Index label

We can drop the DataFrame row by index label using the drop() function. For that we need to pass the index label (which we want to drop from the DataFrame) into this function, it will drop the specified row from the DataFrame. Let’s see with an example.


# Drop the single row using index labels
df1 = df.drop('r2')
print("Drop the row from DataFrame:\n", df1)

Yields below output.

pandas drop row index

As we can see row with the index 'r2' has been dropped from the given DataFrame.

3. Drop Multiple Rows by Index Label

Moreover, using the drop() function we can also drop the multiple rows by index labels from the DataFrame. For, that we need to pass a list of rows into this function, it will drop the specified rows and return the new DataFrame with the remaining rows. For example,


# Drop multiple rows using index labels
df1 = df.drop(['r1', 'r4', 'r5'])
print(df1)

Yields below output.


# Output:
   Courses    Fee Duration  Discount
r2  PySpark  25000   40days      2300
r3   Python  22000   35days      2500

4. Set Inplace = True

Set inplace param as True  and pass it into the drop() function along with a list of rows(which we want to drop from the DataFrame), it will update the existing DataFrame with the remaining rows rather than creating a new DataFrame. Let’s see what will happen,


# Set inplace = True & Drop multiple rows
df.drop(['r1', 'r4', 'r5'], inplace = True)
print(df)

# Output:
#    Courses    Fee Duration  Discount
# r2  PySpark  25000   40days      2300
# r3   Python  22000   35days      2500

5. Pandas Drop Rows by Index Position

Alternatively, using the index position we can select the list of rows(which we want to drop from the DataFrame) from the DataFrame and then, apply the drop() function, which will drop the specified rows and return the new DataFrame with the remaining rows.


# Drop rows using index position
df1 = df.drop([df.index[1], df.index[2]])
print(df1)

# Output:
#    Courses    Fee Duration  Discount
# r1   Spark  20000    30day      1000
# r4  pandas  24000   60days      2000
# r5  Hadoop  30000   55days      3000

6. Pandas Drop the First Row using iloc[]

By using DataFrame.iloc[] attribute you can drop the rows by index. Actually, this function selects the particular portion of a given DataFrame based on its indices and ignores the rest of the portion. The particular portion may be a few rows or a few columns.

For example, use this syntax DataFrame.iloc[n:] to select the first n rows from the DataFrame and then assign it back to the original variable which ideally drops the first n rows from DataFrame. We can also get the first row of DataFrame and get the last row of DataFrame using the Pandas iloc[] property.

Let’s drop the first three rows by the index of the DataFrame using the iloc[] attribute.


# Use DataFrame.iloc[] to drop rows 
df1 = df.iloc[3:]
print(df1) 

Yields below output.


# Output:
   Courses    Fee Duration  Discount
r4  pandas  24000   60days      2000
r5  Hadoop  30000   55days      3000

6. Drop the Row using Index & Name property

Alternatively, we can drop the rows using name property. For, that first, we select the row using the iloc[] property based on its index position then call name property, it will drop the specified row name using the name property.


# Drop rows from DataFrame using index
df1 = df.drop(index=df.iloc[3].name)
print(df1)

Yields below output.


# Output:
   Courses    Fee Duration  Discount
r1    Spark  20000    30day      1000
r2  PySpark  25000   40days      2300
r3   Python  22000   35days      2500
r5   Hadoop  30000   55days      3000

6. Conclusion

In this article, you have learned how to drop the rows by index from Pandas DataFrame using index labels and index position with the help of DataFrame.iloc[], and DataFrame.drop() functions with examples.

Happy Learning !!

References

Vijetha

Vijetha is an experienced technical writer with a strong command of various programming languages. She has had the opportunity to work extensively with a diverse range of technologies, including Python, Pandas, NumPy, and R. Throughout her career, Vijetha has consistently exhibited a remarkable ability to comprehend intricate technical details and adeptly translate them into accessible and understandable materials. Follow me at Linkedin.