Search code examples
pythonarraysreferencefillna

How do I fill an array with 0 (or Nan) using other array as a reference?


I have an array like this:

array(['ENST00000003084', 'ENST00000426809', 'ENST00000454343'], 
['ENST00000003084', 'ENST00000426809', 'ENST00000446805', 'ENST00000454343'], 
['ENST00000003084', 'ENST00000454343'],
['ENST00000003084', 'ENST00000426809', 'ENST00000454343', 'ENST00000600166'])

but I want to all follow the same reference, because they have to maintain this order:

array(['ENST00000003084', 'ENST00000426809', 'ENST00000446805',
       'ENST00000454343', 'ENST00000468795', 'ENST00000600166'])

I don't want just to complete with zeros in the end, I want to fill with 0 the missing values according to the reference array, so they all end up with 6 elements

For example, for the array

['ENST00000003084', 'ENST00000426809', 'ENST00000454343']

the final output should be:

['ENST00000003084', 'ENST00000426809',0, 'ENST00000454343', 0, 0]

Solution

  • this should solve your problem.

    ref = np.array(['ENST00000003084', 'ENST00000426809', 'ENST00000446805',
           'ENST00000454343', 'ENST00000468795', 'ENST00000600166'])
    a = np.array(['ENST00000003084', 'ENST00000426809', 'ENST00000454343'])
    b = [ref[k] if ref[k] in a else 0 for k in range(len(ref))]
    

    The output of this example is

    ['ENST00000003084', 'ENST00000426809', 0, 'ENST00000454343', 0, 0]