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

后端 未结 14 2232
春和景丽
春和景丽 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 16:54

    The pipe functionality can be achieved by composing pandas methods with the dot. Here is an example below.

    Load a sample data frame:

    import seaborn    
    iris = seaborn.load_dataset("iris")
    type(iris)
    # 
    

    Illustrate the composition of pandas methods with the dot:

    (iris.query("species == 'setosa'")
         .sort_values("petal_width")
         .head())
    

    You can add new methods to panda data frame if needed (as done here for example):

    pandas.DataFrame.new_method  = new_method
    

提交回复
热议问题