pandas 체인 프로 그래 밍

669 단어 기초
#       
# 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']))

좋은 웹페이지 즐겨찾기