Functional pipes in python like %>% from R’s magrittr

Pipes are a new feature in Pandas 0.16.2.

Example:

import pandas as pd
from sklearn.datasets import load_iris

x = load_iris()
x = pd.DataFrame(x.data, columns=x.feature_names)

def remove_units(df):
    df.columns = pd.Index(map(lambda x: x.replace(" (cm)", ""), df.columns))
    return df

def length_times_width(df):
    df['sepal length*width'] = df['sepal length'] * df['sepal width']
    df['petal length*width'] = df['petal length'] * df['petal width']
    
x.pipe(remove_units).pipe(length_times_width)
x

NB: The Pandas version retains Python’s reference semantics. That’s why length_times_width doesn’t need a return value; it modifies x in place.

Leave a Comment