我有一个 8 列的数据框,并且想要另一个有 2 列的数据框。这 2 列中的值是根据原始 8 个值计算得出的。
有可能用apply
或transform
吗?
例子:
jnd = pd.DataFrame(np.random.rand(18, 8))
def appl(s):
"""particular processing is not important, only shapes matter.
Therefore just randomly select 2 of passed values"""
return np.random.choice(s, size=2)
jnd.apply(appl, axis=1)
这引发
ValueError: Shape of passed values is (18, 2), indices imply (18, 8)
同样的事情transform
。
您可以将输出转换为Series
创建新列名称的索引:
def appl(s):
"""particular processing is not important, only shapes matter.
Therefore just randomly select 2 of passed values"""
return pd.Series(np.random.choice(s, size=2), index=['a','b'])
print(jnd.apply(appl, axis=1))
a b
0 0.095437 0.256290
1 0.251450 0.072835
2 0.755617 0.630932
3 0.667163 0.449646
4 0.581908 0.341653
5 0.767170 0.376034
6 0.226523 0.120946
7 0.537986 0.385240
8 0.727680 0.998355
9 0.727728 0.308487
10 0.808792 0.286342
11 0.481634 0.767650
12 0.540303 0.106239
13 0.976599 0.640354
14 0.062515 0.062515
15 0.892971 0.856905
16 0.111959 0.526366
17 0.344646 0.268620
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句