Home > Software design >  modifying numpy ndarray by index
modifying numpy ndarray by index

Time:08-10

I'm stuck on trying to add values to the numbers column.

import pandas as pd

def twod_array(num):
    data = {"group": [-1, 0, 1, 2],
            'numbers': [[2], [14, 15], [16, 17], [19, 20, 21]],
            }
    df = pd.DataFrame(data=data)
    print(df)
    return 0

Currently it prints this:

   group       numbers
0     -1           [2]
1      0      [14, 15]
2      1      [16, 17]
3      2  [19, 20, 21]

What I'd like to do is to add a value based on the passed input, so for example if I pass 14.5 as a num, this is the output I'd like to see:

   group       numbers
0     -1           [2]
1      0      [14,14.5 15]
2      1      [16, 17]
3      2  [19, 20, 21]

I'm hoping someone can help with this. This is what I have so far but it fails at the insert line with the error "numpy.ndarray" object has no attribute 'insert'.

df = pd.DataFrame({"group": [-1, 0, 1, 2],
        'numbers': [[2], [14, 15], [16, 17], [19, 20, 21]],
        })
arr = df['numbers'].to_list()
num = 14.5

for i, sub_arr in enumerate(arr):
      for j, n in enumerate(sub_arr):
            if arr[i][j]>num:
                  if j!=0: 
                    arr[i].insert(j,num)
                  else: arr[i-1].insert(-1 ,num)

df['numbers'] = arr

CodePudding user response:

Iterate through the df and see if num lies between the first and last value of the numbers column. If it does, use the bisect module to insert num in a sorted fashion.

import bisect
for i in range(len(df)):
  if num >= df.loc[i,'numbers'][0] and num<= df.loc[i,'numbers'][-1]:
    bisect.insort(df.loc[i,'numbers'],num)
    
print(df)

   group         numbers
0     -1             [2]
1      0  [14, 14.5, 15]
2      1        [16, 17]
3      2    [19, 20, 21]

CodePudding user response:

num = 14.5
mask = (df.numbers.apply(min).lt(num) &
        df.numbers.apply(max).gt(num))
index = mask[mask].index[0]
df.numbers.at[index].append(num)
df.numbers.at[index].sort()
print(df)

# Output:
   group         numbers
0     -1             [2]
1      0  [14, 14.5, 15]
2      1        [16, 17]
3      2    [19, 20, 21]
  • Related