Create a Pipeline and remove a column from DataFrame - Python Pandas

PythonServer Side ProgrammingProgramming

Use the colDrop() method of pdpipe library to remove a column from Pandas DataFrame. At first, import the required pdpipe and pandas libraries with their respective aliases −

import pdpipe as pdp
import pandas as pd

Let us create a DataFrame. Here, we have two columns −

dataFrame = pd.DataFrame(
   {
      "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],"Units": [100, 150, 110, 80, 110, 90]
   }
)

To remove a column from the DataFrame, use the ColDrop() method. Here, we are removing the “Units” column −

resDF = pdp.ColDrop("Units").apply(dataFrame)

Example

Following is the complete code −

import pdpipe as pdp
import pandas as pd

# Create DataFrame
dataFrame = pd.DataFrame(
   {
      "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],"Units": [100, 150, 110, 80, 110, 90]
   }
)

print("DataFrame ...\n",dataFrame)

# removing a row with pdp
dataFrame = pdp.ValDrop(['Jaguar'],'Car').apply(dataFrame)
print("\n DataFrame after removing a row...\n",dataFrame)

# removing a column with pdp
resDF = pdp.ColDrop("Units").apply(dataFrame)
print("\nDataFrame after removing a column...\n",resDF)

Output

This will produce the following output −

DataFrame ...
       Car   Units
0      BMW     100
1    Lexus     150
2     Audi     110
3  Mustang      80
4  Bentley     110
5   Jaguar      90

Displaying after removing a row...
       Car   Units
0      BMW     100
1    Lexus     150
2     Audi     110
3  Mustang      80
4  Bentley     110

Displaying after removing a column...
       Car
0      BMW
1    Lexus
2     Audi
3  Mustang
4  Bentley
raja
Published on 29-Sep-2021 08:26:46
Advertisements