0
0
Pandasdata~10 mins

Dropping columns and rows in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Dropping columns and rows
Start with DataFrame
Choose drop type
Drop rows
Specify labels
Apply drop()
New DataFrame without dropped parts
End
Start with a DataFrame, decide to drop rows or columns by specifying labels, then apply drop() to get a new DataFrame without those rows or columns.
Execution Sample
Pandas
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9]
})

new_df = df.drop('B', axis=1)
This code creates a DataFrame and drops column 'B' to create a new DataFrame without it.
Execution Table
StepActionInputAxisResulting DataFrame
1Create DataFrame{'A':[1,2,3],'B':[4,5,6],'C':[7,8,9]}N/A A B C 0 1 4 7 1 2 5 8 2 3 6 9
2Call drop() to remove column 'B''B'1 (columns) A C 0 1 7 1 2 8 2 3 9
3Call drop() to remove row 110 (rows) A B C 0 1 4 7 2 3 6 9
4Call drop() with multiple columns ['A','C']['A','C']1 (columns) B 0 4 1 5 2 6
5Call drop() with multiple rows [0,2][0,2]0 (rows) A B C 1 2 5 8
6Call drop() with inplace=True to remove column 'C''C'1 (columns)DataFrame df now without column 'C'
7Call drop() with inplace=True to remove row 000 (rows)DataFrame df now without row 0
8ExitNo more dropsN/AFinal DataFrame after drops
💡 All specified rows or columns dropped, no more actions.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7Final
dfOriginal DataFrame with columns A,B,C and rows 0,1,2UnchangedUnchangedUnchangedUnchangedColumn 'C' dropped inplaceRow 0 dropped inplaceDataFrame with columns A,B and rows 1,2
new_dfN/ADataFrame without column 'B'N/AN/AN/AN/AN/AN/A
Key Moments - 3 Insights
Why do we need to specify axis=1 to drop columns?
Because axis=0 means rows and axis=1 means columns. The drop() method uses axis to know if labels are rows or columns. See execution_table step 2 where axis=1 drops column 'B'.
What happens if we don't use inplace=True?
drop() returns a new DataFrame without changing the original. The original stays the same. See variable_tracker where df is unchanged after step 2 but changes only after step 6 with inplace=True.
How to drop multiple rows or columns at once?
Pass a list of labels to drop(), like ['A','C'] for columns or [0,2] for rows. See execution_table steps 4 and 5 for examples.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 2. What does axis=1 mean in the drop() method?
ADrop both rows and columns
BDrop columns
CDrop rows
DNo effect
💡 Hint
Check the 'Axis' column in step 2 of execution_table.
At which step does the original DataFrame 'df' change permanently?
AStep 6
BStep 2
CStep 4
DStep 3
💡 Hint
Look at variable_tracker for 'df' changes after step 6.
If you want to drop row 0 and 2 at once, which step shows this?
AStep 3
BStep 4
CStep 5
DStep 7
💡 Hint
See execution_table rows for multiple rows dropped.
Concept Snapshot
pandas drop() removes rows or columns by label.
Use axis=0 for rows, axis=1 for columns.
Pass label or list of labels to drop.
By default, drop() returns new DataFrame.
Use inplace=True to modify original.
Example: df.drop('B', axis=1) drops column B.
Full Transcript
We start with a DataFrame having columns A, B, and C and rows indexed 0, 1, 2. To remove columns or rows, we use the drop() method. We specify the labels to remove and the axis: axis=1 for columns, axis=0 for rows. For example, dropping column 'B' with df.drop('B', axis=1) returns a new DataFrame without 'B'. If we want to remove rows, we specify row labels and axis=0. We can drop multiple labels by passing a list. By default, drop() returns a new DataFrame and does not change the original. To change the original DataFrame, we use inplace=True. This method helps clean data by removing unwanted rows or columns easily.