Data:

import pandas as pd
dict= {'REF': ['A','B','C','D'],
'ALT': [['E','F'], ['G'], ['H','I','J'], ['K,L']],
'sample1': ['0', '0', '1', '2'],
'sample2': ['1', '0', '3', '0']
}
df = pd.DataFrame(dict)
Problem:
I need to replace the values in columns'Sample1' and 'Sample2'. If there is 0, then 'REF' column value should be placed. If 1, then first element of list in column 'ALT' should be placed, if 2, then second element of 'ALT' column list, and so on.
My Solution:
sample_list = ['sample1', 'sample2']
for sample in sample_list:
#replace 0s
df[sample] = df.apply(lambda x: x[sample].replace('0', x['REF']), axis=1)
#replace other numbers
for i in range(1,4):
try:
df[sample] = df.apply(lambda x: x[sample].replace(f'{i}', x['ALT'][i-1]), axis=1)
except:
pass
However, because list length is different in every 'ALT' column row, it seems that there is IndexError, and values are not replaced after 1. You can see it from the output:

'{"REF":{"0":"A","1":"B","2":"C","3":"D"},"ALT":{"0":["E","F"],"1":["G"],"2":["H","I","J"],"3":["K"]},"sample1":{"0":"A","1":"B","2":"H","3":"2"},"sample2":{"0":"E","1":"B","2":"3","3":"D"}}'
How can I solve it?
UPDATE: If I have NaN value in sample1 or sample2, I can't convert values to int and don't how to skip these values

So, NaN values should not be converted and stayed NaN
Expected output:

