0

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
1
  • Please explain somewhere what your goal is here... as is you have to fill in a lot of blanks and guess what your intention is. Commented Aug 10, 2022 at 2:13

2 Answers 2

1
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]
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! and without looping through the values! Thanks a lot!
0

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.