Python - Create a Pipeline in Pandas


To create a pipeline in Pandas, we need to use the pipe() method. At first, import the required pandas library with an alias −

import pandas as pd

Now, create a DataFrame −

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

Create a pipeline and call the upperFunc() custom function to convert column names to uppercase −

pipeline = dataFrame.pipe(upperFunc)

Following is the upperFun() to convert column names to uppercase −

def upperFunc(dataframe):
# Converting to upppercase
   dataframe.columns = dataframe.columns.str.upper()
   return dataframe

Example

Following is the complete code −

import pandas as pd

# function to convert column names to uppercase
def upperFunc(dataframe):
   # Converting to upppercase
   dataframe.columns = dataframe.columns.str.upper()
   return dataframe

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

print"DataFrame ...\n",dataFrame

# creating pipeline using pipe()
pipeline = dataFrame.pipe(upperFunc)

# calling pipeline
print"\nDisplaying column names in uppercase...\n",pipeline

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 column names in uppercase...
       CAR   UNITS
0      BMW    100
1    Lexus    150
2     Audi    110
3  Mustang     80
4  Bentley    110
5   Jaguar     90

Updated on: 15-Sep-2021

189 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements