python-3.x - Python,使多个(点)看起来更干净
问题描述
对于一般的python或者可能只是pandas,有没有更好或更清洁的方法来做多个.
(点)?例如,假设我有一个这样的代码片段:
df = pd.melt(df, id_vars='bar')
df.dropna(inplace=True)
df.drop('variable', axis=1, inplace=True)
df.reset_index(drop=True, inplace=True)
df.rename(columns={'value': 'foo'}, inplace=True)
我怎样才能让这样的东西看起来更干净?像这样:
df = pd.melt(df, id_vars='bar')
.dropna(inplace=True)
.drop('variable', axis=1, inplace=True)
.reset_index(drop=True, inplace=True)
.rename(columns={'value': 'foo'}, inplace=True)
解决方案
这完全取决于函数返回什么。该df
对象具有这些方法,但这些方法返回的却没有。因此,除非您修改 Panda 的代码,否则没有严格的方法可以“使点看起来更好”。这些方法必须返回一个具有相同方法的对象,因此摆脱inplace=True
参数也将修复它。None
否则熊猫的回报。
本质上,inplace
是修改df
对象而不返回它。否则,这些方法将返回具有相同方法的对象,因此请尝试:
df = (
pd.melt(df, id_vars='bar')
.dropna()
.drop('variable', axis=1)
.reset_index(drop=True)
.rename(columns={'value': 'foo'})
)
推荐阅读
- google-analytics - 如何在多语言网站的 Google Tag Manager dataLayer 中描述产品类别和产品名称?
- java - 是否可以使用 Java JRE 11 运行 Java 小程序?
- android - 如何在 MPAndroidChart 中绘制单元格
- java - 在可能的情况下,是否应该消除 SQL 查询以支持使用 Hibernate?
- php - 在codeigniter中上传多个文件并使用数组重命名每个文件
- android - AndroidX - 程序类型已经存在:androidx.annotation.BoolRes
- javascript - 等待所有元素完全使用 Promise.all
- mysql - 使用中间表用相关表的总和值更新一个表的值
- google-cloud-platform - 无法针对 BigQuery 运行查询 - 权限错误 403
- android - 画廊而不是Android中的相机活动(Tensorflow)