Given the following list:
a = ['1', '2', '12', '5']
I am trying to delete all values from the Dataframes cells which don't match the elements in the list. I know for sure that every value from the list appears only once in each row of the Dataframe.
test = pd.DataFrame({'0' : ['1','4','5','5'],
'1' : ['4','1','12','10'],
'2' : ['10','12','4','2'],
'3' : ['2','10','10','4'],
'4' : ['5','2','2','1'],
'5' : ['12','5','1','12']})
0 1 2 3 4 5
0 1 4 10 2 5 12
1 4 1 12 10 2 5
2 5 12 4 10 2 1
3 5 10 2 4 1 12
The important aspect is to maintain the order of the matching numbers from the list in the Dataframe.
The result should look like this:
0 1 2 3
0 1 2 5 12
1 1 12 2 5
2 5 12 2 1
3 5 2 1 12
Thanks in advance!