Create a Pipeline and remove a row from an already created DataFrame - Python Pandas

PythonServer Side ProgrammingProgramming

Use the ValDrop() method of pdpipe library to remove a row from an already create 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]
   }
)

Now, remove a row using valdDrop() method −

dataFrame = pdp.ValDrop(['Jaguar'],'Car').apply(dataFrame)

Example

Following is the complete code −

import pdpipe as pdp
import pandas as pd

# function to check for excess units
def demo(x):
   if x >= 100:
      return "OverStock"
   else:
      return "UnderStock"

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

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

# adding a new column "Stock" and its values based on an already created column "Units"
dataFrame['Stock'] = dataFrame['Units'].apply(demo)

print("\n DataFrame with a new column...\n",dataFrame)

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

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

DataFrame with a new column...
       Car   Units        Stock 
0      BMW     100    OverStock
1    Lexus     150    OverStock
2     Audi     110    OverStock
3  Mustang      80   UnderStock
4  Bentley     110    OverStock
5   Jaguar      90   UnderStock

DataFrame after removing a value...
       Car   Units        Stock
0      BMW     100    OverStock
1    Lexus     150    OverStock
2     Audi     110    OverStock
3  Mustang      80   UnderStock
4  Bentley     110    OverStock
raja
Published on 01-Oct-2021 11:22:19
Advertisements