pandas 链式编程

# 链式编程技术
# Usual non-functional way
df2 = df.copy()
df2['k'] = v

# Functional assign way
df2 = df.assign(k=v)

# 就地分配可能会比assign快,但是assign可以方便地进行链式编程
# 使用外括号,这样便于添加换行符
r = (df2.assign(col1_demeaned = df2.col1 -df2.col2.mean())
     .group('key')
     .col1_demeaned.std())

#=====管道======
#想转换多列
def group_demean(df, by, cols):
    result = df.copy()
    g = df.groupby(by)
    for c in cols:
        result[c] = df[c] - g[c].transform('mean')
    return result

#管道链条式操作
# f 为function
# f(df)和df.pipe(f)是等价的
result = (df[df.col1 < 0]
          .pipe(group_demean, ['key1', 'key2'], ['col1']))

猜你喜欢

转载自blog.csdn.net/scc_hy/article/details/80048687