问题描述
我有一长串的熊猫链命令,例如:
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(ascending=False)
我希望能够跨多行显示它,但仍将其作为一个衬纸显示(不将结果保存到临时对象,也无需将lambda定义为函数)
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)
一个我希望它看起来的例子:
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)
是否可以这样做? (我知道'_'在python中具有此功能,但似乎不适用于链接的命令)
Is it possible to do so? (I know '_' has this functionality in python, but it doesn't seem to work with chained commands)
推荐答案
在python中,您可以通过以反斜杠结束行或将表达式括在括号中来继续下一行.
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)
或
(df.groupby[['x','y']]
.apply(lambda x: (np.max(x['z'])-np.min(x['z'])))
.sort_values(ascending=False))
这篇关于是否可以将多条 pandas 命令序列拆分为多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!