为数据集中的所有行应用一组步骤,以创建新的数据集

2024-04-18 18:59:21 发布

您现在位置:Python中文网/ 问答频道 /正文

对于数据集,我在第一行应用了一组步骤来生成新行。我需要对整个数据集(其余行)应用这组步骤。手动将花费很长时间,我尝试编写一个for循环,它将在行的长度范围内工作(包括最后一行)。但是我犯了一个错误。谢谢你的帮助。多谢各位

Set of steps are :

  1. For first part

    • take the first non-zero entry out,
    • permutate the remaining 20 entries
    • then multiply by them by the non zero.
  2. For the second part

    • applying a mapping that converts the remaining 20 entries (unpermutated version) by using a mapping into a single number and then multiply by [1x20] matrix/array with only one "1" and rest "0".
  3. Then finally
    • add the result of the first and the second part to print a [1x20] matrix.

我设法对第一行执行此操作,但我想对所有483行执行此操作,但我无法执行此操作。)

for i in range(len(df0.index)+1):

    a=df0.iloc[i].values
    y= []
    for i in range(len(a)):

        if a[i] != b[0]:  y.append(a[i])
    y

    import itertools
    d=(list(itertools.permutations(y[0:7])))
    p1=random.choice(d)

    e=(list(itertools.permutations(y[7:14])))
    p2=random.choice(e)

    f=(list(itertools.permutations(y[14:])))
    p3=random.choice(f)

    permutation=np.concatenate([p1, p2, p3])
    permutation

    xpi=b[0]*permutation

    y1=np.array(y[:7])
    h1=((y1[0] *y1[1]) + (y1[2] * y1[3]) +(y1[4] *y1[5])) * y1[6]

    y2=np.array(y[7:14])
    h2=((y2[0] *y2[1]) + (y2[2] * y2[3]) +(y2[4] *y2[5])) * y2[6]

    y3=np.array(y[14:])
    h3=(y3[0]*y3[1]*y3[2])+(y3[3]*y3[4]*y3[5])

    h=h1+h2+h3
    i=np.array([1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
    hy=h*i

    fxy= xpi+hy
print(fxy)
ValueError                                Traceback (most recent call last)
<ipython-input-116-103cb4f4f61d> in <module>
     36     hy=h*i
     37 
---> 38     fxy= xpi+hy
     39 print(fxy)

ValueError: operands could not be broadcast together with shapes (21,) (20,)

Tags: andtheforbynparrayfirstprint