Is it possible to split a sequence of pandas commands across multiple lines?

后端 未结 3 689
耶瑟儿~
耶瑟儿~ 2021-02-04 01:36

I have a long string of pandas chained commands, for example:

df.groupby[[\'x\',\'y\']].apply(lambda x: (np.max(x[\'z\'])-np.min(x[\'z\']))).sort_values(ascendin         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-04 01:59

    In python you can continue to the next line by ending your line with a reverse slash or by enclosing the expression in parenthesis.

    df.groupby[['x','y']] \
    .apply(lambda x: (np.max(x['z'])-np.min(x['z']))) \
    .sort_values(ascending=False)
    

    or

    (df.groupby[['x','y']]
    .apply(lambda x: (np.max(x['z'])-np.min(x['z'])))
    .sort_values(ascending=False))
    

提交回复
热议问题