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

后端 未结 3 690
耶瑟儿~
耶瑟儿~ 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:58

    Since this has the nature of a command, I would probably format it close to your example, like this:

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

    It took me a long time to realize I could break these expressions before the dots, which is often more readable than breaking inside the parentheses (same goes for "some long string".format()).

    If this were more like an expression evaluation, I'd wrap the whole thing in parentheses, which is considered more "Pythonic" than line continuation markers:

    var = (
        df.groupby[['x','y']]
            .apply(
                lambda x: np.max(x['z'])-np.min(x['z'])
            ) 
            .sort_values(ascending=False)
    )
    
    0 讨论(0)
  • 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))
    
    0 讨论(0)
  • 2021-02-04 01:59

    The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation

    from https://www.python.org/dev/peps/pep-0008/#id19

    So may be better:

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

    The last printed expression variable "_" is known only in the Python console, so without explicit attribution cannot be used for that purpose in a script/module.

    0 讨论(0)
提交回复
热议问题