Site icon Spark By {Examples}

Pandas Drop Rows by Index

pandas drop row 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.

Key Points –

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.


# Quick examples of drop rows by index

# 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)

To run some examples of pandas drop rows by index, let’s create pandas DataFrame from Dictionary.


# 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

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.

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

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

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

Pandas Drop the First Row using iloc[]

To drop the first row using iloc[], you can specify the index range from the second row onwards. For instance, use DataFrame.iloc[1:] to select all rows from index position 1 (inclusive) onwards. By specifying [1:], you’re effectively excluding the first row from the DataFrame.


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

Yields below output.


# Output:
    Courses    Fee Duration  Discount
r2  PySpark  25000   40days      2300
r3   Python  22000   35days      2500
r4   pandas  24000   60days      2000
r5   Hadoop  30000   55days      3000

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

FAQ on Pandas Drop Rows by Index

How can I drop rows by index in a pandas DataFrame?

You can drop rows by index in a pandas DataFrame using the DataFrame.drop() method or by using integer-based indexing with DataFrame.iloc[].

What’s the difference between DataFrame.drop() and DataFrame.iloc[] for dropping rows?

DataFrame.drop() is a method that allows you to drop rows by their index labels or index positions. DataFrame.iloc[], on the other hand, is primarily used for integer-based indexing, allowing you to select rows and columns by their integer positions.

Can I drop rows by index positions and reassign it back to the original DataFrame?

You can drop rows by index positions using DataFrame.iloc[] and assign the result back to the original DataFrame variable.

Is it possible to drop multiple rows by index positions using DataFrame.iloc[]?

It is possible to drop multiple rows by index positions using DataFrame.iloc[]. You can specify a list of index positions corresponding to the rows you want to drop.

Conclusion

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

Happy Learning !!

References

Exit mobile version