Functional pipes in python like %>% from R's magritrr

后端 未结 14 2234
春和景丽
春和景丽 2020-11-29 16:17

In R (thanks to magritrr) you can now perform operations with a more functional piping syntax via %>%. This means that instead of coding this: <

14条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 16:53

    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.

提交回复
热议问题