是否可以将一系列 Pandas 命令拆分为多行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33944522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to split a sequence of pandas commands across multiple lines?
提问by user2808117
I have a long string of pandas chained commands, for example:
我有一长串 Pandas 链式命令,例如:
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 across multiple lines but still as a one liner (without saving results to a temporary object, or defining the lambda as a function)
我希望能够跨多行呈现它,但仍然作为一个班轮(不将结果保存到临时对象,或将 lambda 定义为函数)
an example of how I would like it to look:
我希望它看起来如何的一个例子:
df.groupby[['x','y']]
.apply(lambda x: (np.max(x['z'])-np.min(x['z'])))
.sort_values(ascending=False)
Is it possible to do so? (I know '_' has this functionality in python, but it doesn't seem to work with chained commands)
有可能这样做吗?(我知道 '_' 在 python 中有这个功能,但它似乎不适用于链式命令)
回答by GaryBishop
In python you can continue to the next line by ending your line with a reverse slash or by enclosing the expression in parenthesis.
在 python 中,您可以通过以反斜杠结束行或将表达式括在括号中来继续下一行。
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))
回答by Zoli
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
包装长行的首选方法是在括号、方括号和大括号内使用 Python 的隐含行继续符。通过将表达式括在括号中,可以将长行分成多行。这些应该优先于使用反斜杠进行行继续
from https://www.python.org/dev/peps/pep-0008/#id19
来自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.
最后打印的表达式变量“_”仅在 Python 控制台中已知,因此如果没有明确的属性,则不能在脚本/模块中用于该目的。
回答by Matthias Fripp
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()).
我花了很长时间才意识到我可以在点之前打破这些表达式,这通常比在括号内打破更具可读性(“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:
如果这更像是一个表达式评估,我会将整个内容括在括号中,这被认为比行继续标记更“Pythonic”:
var = (
df.groupby[['x','y']]
.apply(
lambda x: np.max(x['z'])-np.min(x['z'])
)
.sort_values(ascending=False)
)