Is it possible to split a sequence of pandas commands into several lines?

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

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

And I would like to be able to present it in several lines, but still as one liner (without saving the results for a temporary object or defining a lambda as a function)

An example of how I would like to look:

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

Can this be done? (I know that "_" has this functionality in python, but doesn't seem to work with chain commands)

+6
source share
3 answers

In python, you can continue the next line by ending your line with a backslash or by including the expression in parentheses.

 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)) 
+7
source

The preferred way to wrap long lines is to use Python's implied line continuation in parentheses, brackets, and curly braces. Long lines can be split into multiple lines by wrapping expressions in parentheses. They should be used instead of using a backslash to continue the line.

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

So it could be better:

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

The last variable of the print variable "_" is known only in the Python console, so without explicit attribution it cannot be used for this purpose in script / module.

+1
source

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

 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 that I could break these expressions before the dots, which is often more readable than breaking in parentheses (the same goes for β€œsome long string .format ()).

If it was more like evaluating an expression, I would have wrapped it all in parentheses, which are 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
source

All Articles