How to Remove Rows and Columns from DataFrame Using Pandas drop() function

Table of Content

  1. Introduction
  2. Syntax and Parameters
  3. How to use .drop() func?
  4. Error Handling
  5. How to use .drop() based on conditions
  6. Common Mistakes and Fixes
  7. Sorting
  8. Handling Missing Data
  9. Aggregations & Statistics
  10. Grouping & Pivoting
  11. String Methods (.str)
  12. Datetime Handling (.dt)
  13. Reshaping
  14. Merging & Joining
  15. Apply & Lambda

​1. Introduction

When working with DataFrames in Pandas, you often need to remove unnecessary rows or columns — maybe a feature you don’t need, a column of missing data, or an incorrect row.


Python gives you a simple yet powerful function for this:   drop() 

⭐ What is drop() Function?

The .drop() function is used to remove rows or columns from a DataFrame.


By using  axis='columns' or columns= the drop() function deletes the column you mention.

By using  axis='index' or index=the drop() function deletes the row you mention.

​2. Syntax and Parameters

# Syntax for drop() function
DataFrame.drop(labels=None, *, axis="0," index="None," columns="None," level="None," inplace="False," errors='raise')

These parameters — labels, axis, index, columns, level, inplace, and errors — are provided as Keyword Arguments.

ParameterDescription
labelsRow index or column name(s) to drop (depends on axis).
axisChoose what to drop: 0 = rows, 1 = columns.
 indexDirectly specify row index(es) to remove.
 columnsDirectly specify column name(s) to remove.
 levelUsed for MultiIndex; defines which index level to target.
 inplaceTrue:change original DataFrame.
False:return new one.
errors 'raise': show error if label not found.
'ignore': skip missing labels.

​​​3. How to use .drop() function?

Let's consider an example with a simple dataframe to use .drop() function — We’ll keep using this same example for the following actions.

#import pandas library
import pandas as pd

df = pd.DataFrame({
    'Name': ['Amit', 'Riya', 'Sam'],
    'Age': [21, 22, 20],
    'Marks': [85, 90, 88]
})
print(df)

⚡️ How to Drop a Column from a DataFrame?

# Drop a column from a DataFrame
df_new = df.drop(columns=['Marks'])

Code Explanation

df.drop() - tell pandas to delete the column named Marks.

df_new - stores the updated DataFrame without that column.

⛳ Output:

# Updated DataFrame without 'Marks' column
   Name  Age
0  Amit   21
1  Riya   22
2   Sam   20

⚡️ How to Drop multiple Columns from a DataFrame?

# Drop multiple columns from a DataFrame
df_new = df.drop(columns=['Age', 'Marks'])

Code Explanation

df.drop() - tell pandas to delete the columns named Age and Marks

df_new - stores the updated DataFrame without that column.

# Updated DataFrame without 'Age' and 'Marks' column
   Name
0  Amit
1  Riya
2   Sam

⚡️ How to Drop a Row from a DataFrame?

# Drop a row from a DataFrame
df_new = df.drop(index=[1])

Code Explanation

df.drop() - tell pandas to delete the row with index=1

df_new - stores the updated DataFrame without that row.

# Updated DataFrame without index 1
      Name  Age  Marks
0  Amit   21     85
2   Sam   20     88

⚡️ How to Drop multiple Rows from a DataFrame?

# Drop multiple rows from a DataFrame
df_new = df.drop(index=[0, 2])

Code Explanation

df.drop() - tell pandas to delete the row with index 1 and 3

df_new - stores the updated DataFrame without that row.

# Updated DataFrame without 'Age' and 'Marks' column
      Name  Age  Marks
1  Riya   22     90

4. Error Handling

Sometimes you may try to drop a row or column that does not exist in the DataFrame.
By default, Pandas gives an error - KeyError: "['SomeLabel'] not found in axis" 

To avoid the error, you can use:  errors='ignore' 

This tells Pandas:

👉 “If the label is missing, don’t stop the program — just continue.”

# Try dropping a column that does NOT exist
df_new = df.drop(columns=['City'], errors='ignore')

Code Explanation

  • The DataFrame has only Name, Age and Marks columns.
  • We attempt to drop “City”, which does not exist.
  • Instead of giving an error, errors='ignore' allows the code to run smoothly.
  • Since the column is not found, the DataFrame stays the same.

# Updated DataFrame
    Name  Age  Marks
0  Amit   21     85
1  Riya   22     90
2   Sam   20     88

5. How to drop rows and columns based on Conditions?

In real data cleaning, you often delete rows and columns based on conditions — for example:
  • Drop rows where Age < 21
  • Drop rows where Marks < 40
  • Drop rows where Name is missing
  • Drop rows containing outliers


Pandas allows you to do this easily using Boolean Filtering (True/False condition) - a method which keeps only the rows where the condition is True and removes the rows where the condition is False.

Example: Drop Rows Where Marks < 90

# Drop rows where Marks are less than 90
df_new = df[df['Marks'] >= 90]

Code Explanation

  •  df['Marks'] >= 90  creates a condition
  • Only rows where this condition is True are kept
  • Rows where Marks < 90 (Amit and Sam) are removed

# Updated DataFrame where Marks are less than equal to 90
    Name  Age  Marks
1  Riya   22     90

⭐ Drop Rows Using Multiple Conditions in Pandas

You can combine multiple conditions using:

  • & → AND

  • | → OR

  • ~ → NOT

Example: Keep rows where Age > 20 AND Marks > 85

# Drop rows where Marks are less than 90
df_new = df[(df['Age'] > 20) & (df['Marks'] > 85)]

Code Explanation

  • (df['Age'] > 20)  Condition 1: Age > 20 → True for Amit and Riya
  • (df['Marks'] > 85)  Condition 2: Marks > 85 → True for Riya and Sam

  • Using &, only rows where both conditions are True are kept - Only Riya satisfies both

# Updated DataFrame where Age > 20 AND Marks > 85
    Name  Age  Marks
1  Riya   22     90

6. Common Mistakes and Fixes

1️⃣ Dropping non-existing column → KeyError


❌ Mistake:
df.drop(columns=['City'])


✅ Fix:
df.drop(columns=['City'], errors='ignore')


2️⃣ Wrong axis when dropping columns


❌ Mistake:

df.drop('Marks')


✅ Fix:
df.drop(columns=['Marks']) or df.drop('Marks', axis="1)


3️⃣ Missing parentheses in multiple conditions


❌ Mistake:
df[df['Age'] > 20 & df['Marks'] > 85]


✅ Fix:
df[(df['Age'] > 20) & (df['Marks'] > 85)]


4️⃣ Using and / or instead of & / |


❌ Mistake:
df[(df['Age'] > 20) and (df['Marks'] > 85)]


✅ Fix:
df[(df['Age'] > 20) & (df['Marks'] > 85)]


5️⃣ Expecting drop() to modify original DataFrame


❌ Mistake:
df[(df['Age'] > 20) and (df['Marks'] > 85)]


✅ Fix:
df[(df['Age'] > 20) & (df['Marks'] > 85)]

Summary

1. Import & Setup
  • import pandas as pd
  • pd.__version__ → check version
  • pd.set_option('display.max_rows', n) → control display

1. what is .drop() function? - It removes the specified row or column
    • axis='columns' or columns=...  → Removes columns

    • axis='index' or index=...  → Removes rows

3. Input/Output (I/O)
  • pd.read_csv('file.csv')
  • pd.read_excel('file.xlsx')
  • pd.read_json('file.json')
  • pd.read_sql(query, connection)
  • df.to_csv('file.csv')
  • df.to_excel('file.xlsx')

4. Inspecting Data
df.head(n) / df.tail(n)
df.info()
df.shape
df.dtypes
df.columns
df.index
df.describe()

5. Selection & Indexing
df['col'] → select column
df[['col1','col2']] → multiple columns
df.loc[row_index, col_name] → label-based
df.iloc[row_index, col_index] → position-based
df.at[row, col] / df.iat[row, col] → fast scalar access

6. Filtering & Boolean Indexing
df[df['col'] > value]
df.query('col > value')
df[(df['A'] > 0) & (df['B'] < 5)]

7. Sorting
df.sort_values('col')
df.sort_values(['col1','col2'], ascending="[True," False])
df.sort_index()

8. Handling Missing Data
df.isnull() / df.notnull()
df.dropna()
df.fillna(value)
df.interpolate()

9. Aggregations & Statistics
df.sum() / df.mean() / df.median()
df.min() / df.max()
df.std() / df.var()
df.count()
df.value_counts()
df.corr()

10. Grouping & Pivoting
df.groupby('col').mean()
df.groupby(['A','B']).agg({'C':'sum'})
df.pivot(index='col1', columns='col2', values='col3')
df.pivot_table(values='col', index='A', columns='B', aggfunc='mean')

11. String Methods (.str)
df['col'].str.lower() / .upper()
df['col'].str.contains('text')
df['col'].str.replace('a','b')
df['col'].str.len()

12. Datetime Handling (.dt)
pd.to_datetime(df['date'])
df['date'].dt.year / .month / .day
df['date'].dt.weekday
df['date'].dt.strftime('%Y-%m-%d')

13. Reshaping
df.melt(id_vars, value_vars)
df.stack() / df.unstack()
df.pivot_table()

14. Merging & Joining
pd.concat([df1, df2])
pd.merge(df1, df2, on='key')
pd.merge(df1, df2, how='left')
df1.join(df2)

15. Apply & Lambda
df['col'].apply(func)
df.applymap(func) → elementwise on DataFrame
df.transform(lambda x: x+1)

16. Attributes (quick access)
df.shape → (rows, cols)
df.index → row index
df.columns → column names
df.dtypes → data types
df.size → total elements
df.values → underlying NumPy array

17. Export & Save
df.to_csv('data.csv')
df.to_excel('data.xlsx')
df.to_json('data.json')
df.to_sql(table, connection)

18. Visualization (basic)
df.plot()
df['col'].hist()
df.plot.scatter(x='col1', y='col2')