gpt4 book ai didi

python - ValueError : Shape of passed values is X, 索引暗示 Pandas 应用和转换中的 Y

转载 作者:太空宇宙 更新时间:2023-11-03 14:26:42 26 4
gpt4 key购买 nike

我有 8 列数据框,想要另一个 2 列数据框。这 2 列中的值是根据原始 8 个值计算得出的。

可以使用applytransform吗?

示例:

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)

转换也是如此。

最佳答案

您可以使用创建新列名称的索引将输出转换为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

关于python - ValueError : Shape of passed values is X, 索引暗示 Pandas 应用和转换中的 Y,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47592386/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com