是否可以将多条 pandas 命令序列拆分为多行? [英] Is it possible to split a sequence of pandas commands across multiple lines?

查看:95
本文介绍了是否可以将多条 pandas 命令序列拆分为多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一长串的熊猫链命令,例如:

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 命令序列拆分为多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆