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

Python | Delete rows/columns from DataFrame

Deleting rows or columns from a pandas DataFrame is a common operation. Here's a step-by-step tutorial on how to do it:

1. Setup:

Firstly, ensure you have the required library installed:

pip install pandas

2. Import the necessary library:

import pandas as pd

3. Create a Sample DataFrame:

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)

4. Delete Rows:

a. Using 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)

b. Using Boolean Indexing:

Let's say we want to remove rows where column 'A' is less than 3:

df = df[df['A'] >= 3]
print(df)

5. Delete Columns:

a. Using 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)

b. Using del keyword:

To delete column 'B':

del df['B']
print(df)

6. Using 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)

7. Resetting Index:

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.

Summary:

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.

  1. Pandas DataFrame drop rows by index:

    • Use the drop() method to remove rows based on their index.
    • Example:
      # Drop rows by index
      df = df.drop([0, 1])
      
  2. Delete columns from Pandas DataFrame:

    • Use the drop() method with axis=1 to remove columns by name.
    • Example:
      # Delete columns by name
      df = df.drop(['Column1', 'Column2'], axis=1)
      
  3. Remove rows with specific condition in Pandas DataFrame:

    • Filter rows based on a condition and create a new DataFrame.
    • Example:
      # Remove rows with specific condition
      df = df[df['Column'] != 'value']
      
  4. Python DataFrame dropna method:

    • Use the dropna() method to remove rows with missing values.
    • Example:
      # Drop rows with missing values
      df = df.dropna()
      
  5. Drop duplicate rows in Pandas DataFrame:

    • Use drop_duplicates() to remove duplicate rows.
    • Example:
      # Drop duplicate rows
      df = df.drop_duplicates()
      
  6. How to delete a row by condition in Pandas:

    • Use a boolean condition to filter rows and create a new DataFrame.
    • Example:
      # Delete row by condition
      df = df[df['Column'] != 'value']
      
  7. Drop columns with certain values in Pandas DataFrame:

    • Filter columns based on values and create a new DataFrame.
    • Example:
      # Drop columns with certain values
      df = df.loc[:, ~df.columns.isin(['Column1', 'Column2'])]
      
  8. Pandas DataFrame drop columns by name:

    • Use the drop() method with axis=1 to remove columns by name.
    • Example:
      # Drop columns by name
      df = df.drop(['Column1', 'Column2'], axis=1)
      
  9. Delete empty rows in Python DataFrame:

    • Use the dropna() method to remove rows with missing values.
    • Example:
      # Delete empty rows
      df = df.dropna()
      
  10. Drop rows based on multiple conditions in Pandas:

    • Combine multiple conditions using logical operators for row removal.
    • Example:
      # Drop rows based on multiple conditions
      df = df[(df['Column1'] != 'value') & (df['Column2'] > 10)]