Pandas Tutorial
Creating Objects
Viewing Data
Selection
Manipulating Data
Grouping Data
Merging, Joining and Concatenating
Working with Date and Time
Working With Text Data
Working with CSV and Excel files
Operations
Visualization
Applications and Projects
Deleting rows or columns from a pandas DataFrame is a common operation. Here's a step-by-step tutorial on how to do it:
Firstly, ensure you have the required library installed:
pip install pandas
import pandas as pd
For the tutorial, let's use this sample DataFrame:
data = { 'A': [1, 2, 3, 4, 5], 'B': [10, 20, 30, 40, 50], 'C': ['p', 'q', 'r', 's', 't'] } df = pd.DataFrame(data) print(df)
drop
method:To delete, for example, the row with index 2:
df = df.drop(2) print(df)
To delete multiple rows, say with index 1 and 3:
df = df.drop([1, 3]) print(df)
Let's say we want to remove rows where column 'A' is less than 3:
df = df[df['A'] >= 3] print(df)
drop
method:To delete column 'A':
df = df.drop(columns=['A']) print(df)
To delete multiple columns, say 'A' and 'C':
df = df.drop(columns=['A', 'C']) print(df)
del
keyword:To delete column 'B':
del df['B'] print(df)
inplace
Parameter:For all the deletions we did above, we assigned the result back to df
. Instead, you can use the inplace
parameter of the drop
method to modify the DataFrame in place:
df.drop(columns=['A'], inplace=True)
After deleting rows, you may want to reset the index of your DataFrame:
df.reset_index(drop=True, inplace=True) print(df)
Here, the drop=True
argument ensures that the old index is discarded and not added as a new column.
Deleting rows or columns in a pandas DataFrame is straightforward. The drop
method is versatile and allows for easy removal of rows/columns. Remember to use the inplace
parameter if you don't wish to assign the result back to the DataFrame. Always handle your data with care to ensure you don't accidentally remove critical information.
Pandas DataFrame drop rows by index:
drop()
method to remove rows based on their index.# Drop rows by index df = df.drop([0, 1])
Delete columns from Pandas DataFrame:
drop()
method with axis=1
to remove columns by name.# Delete columns by name df = df.drop(['Column1', 'Column2'], axis=1)
Remove rows with specific condition in Pandas DataFrame:
# Remove rows with specific condition df = df[df['Column'] != 'value']
Python DataFrame dropna method:
dropna()
method to remove rows with missing values.# Drop rows with missing values df = df.dropna()
Drop duplicate rows in Pandas DataFrame:
drop_duplicates()
to remove duplicate rows.# Drop duplicate rows df = df.drop_duplicates()
How to delete a row by condition in Pandas:
# Delete row by condition df = df[df['Column'] != 'value']
Drop columns with certain values in Pandas DataFrame:
# Drop columns with certain values df = df.loc[:, ~df.columns.isin(['Column1', 'Column2'])]
Pandas DataFrame drop columns by name:
drop()
method with axis=1
to remove columns by name.# Drop columns by name df = df.drop(['Column1', 'Column2'], axis=1)
Delete empty rows in Python DataFrame:
dropna()
method to remove rows with missing values.# Delete empty rows df = df.dropna()
Drop rows based on multiple conditions in Pandas:
# Drop rows based on multiple conditions df = df[(df['Column1'] != 'value') & (df['Column2'] > 10)]