【发布时间】:2019-07-30 03:54:57
【问题描述】:
我有一个数据框 df
df:
GROUP VALUE
1 5
2 2
1 10
2 20
1 7
还有一个函数
import numpy as np
from scipy import stats
def z_score(x):
z = np.abs(stats.zscore(x))
c = np.where(x > 5, 1, 0)
return z,c
我正在尝试借助函数输出和 pandas 转换方法在数据框中创建两列
df['zscore'], df['label'] = a.groupby(['GROUP'])['VALUE'].transform(z_score)
但是运行上面的sn-p后出现如下错误
ValueError: Length of passed values is 2, index implies 3
如何做到这一点?
【问题讨论】:
标签: python pandas transform apply pandas-groupby