Home > Blockchain >  iterating different length arrays and replace values
iterating different length arrays and replace values

Time:05-13

I have a dataframe that looks like this:

df = pd.DataFrame({'col1': [[[1,5,3],[0,0,0]], [[1,2,3],[0,0,0], [1,2,3]]]})

# which looks like this:
    col1
0   [[1, 5, 3], [0, 0, 0]]
1   [[1, 2, 3], [0, 0, 0], [1, 2, 3]]

I also have another array (a):

a = np.array([[1], [2], [3], [4], [5]])

I want to replace the value in every second position from df with values from a. So for example, I want to make dataframe that looks like this (every second value is changed from each array):

    col1
0   [[1, 1, 3], [0, 2, 0]]
1   [[1, 3, 3], [0, 4, 0], [1, 5, 3]]

This is how I approached it:

for i in range(len(df)):
    temp = df.loc[i, "col1"]
    for k in range(len(a)):
        for j in range(len(temp)):
            temp[j][1] = a[k][0]
    print(temp)

which result in :

[[1, 5, 3], [0, 5, 0]]
[[1, 5, 3], [0, 5, 0], [1, 5, 3]]

Can anyone please help me how to figure this problem out?? Thanks in advance!

CodePudding user response:

You shouldn't be looping over a with a nested loop, as that will create a cross product. Just increment an index variable and use that to get the next element of the array.

k = 0
for i in range(len(df)):
    temp = df.loc[i, "col1"]
    for j in range(len(temp)):
        temp[j][1] = a[k][0]
        k  = 1
    print(temp)

CodePudding user response:

A simple way could be to transform the array into an iterator:

i = iter(a.ravel())
for L in df['col1']:
    for l in L:
        l[1] = next(i, l[1])

NB. using here the original value as default in case the array is shorter than the number of sublists

output:

                                col1
0             [[1, 1, 3], [0, 2, 0]]
1  [[1, 3, 3], [0, 4, 0], [1, 5, 3]]
  • Related