1

I have an np.array I would like to remove specific elements from based on the element "name" and not the index. Is this sort of thing possible with np.delete() ?

Namely my original ndarray is

textcodes= data['CODES'].unique()

which captures unique text codes given the quarter.

Specifically I want to remove certain codes which I need to run through a separate process and put them into a separate ndarray

sep_list = np.array(['SPCFC_CODE_1','SPCFC_CODE_2','SPCFC_CODE_3','SPCFC_CODE_4])

I have trouble finding a solution on removing these specific codes in sep_list from textcodes because I don't know exactly where these sep_list codes would be indexed as it would be different each quarter and I would like to automate it based on the specific names instead because those will always be the same.

Any help is greatly appreciated. Thank you.

1 Answer 1

1

You should be able to do something like:

import numpy as np

    data = [3,2,1,0,10,5]
    bad_list = [1, 2]
    data = np.asarray(data)
    new_list = np.asarray([x for x in data if x not in bad_list])

    print("BAD")
    print(data)
    print("GOOD")
    print(new_list)

Yields:

BAD
[ 3  2  1  0 10  5]
GOOD
[ 3  0 10  5]

It is impossible to tell for sure since you did not provide sample data, but the following implementation using your variables should work:

import numpy as np

textcodes= data['CODES'].unique()
sep_list = np.array(['SPCFC_CODE_1','SPCFC_CODE_2','SPCFC_CODE_3','SPCFC_CODE_4'])

final_list = [x for x in textcodes if x not in sep_list]
Sign up to request clarification or add additional context in comments.

1 Comment

No problem, @mattie_g. Good luck coding. If this answer helped you, please make sure to accept it so that others who have similar problems can reference it too. This helps make Stack Overflow a productive resource of programming Q&A :) (not to mention, you get more reputation points when you accept an answer!)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.