'Start looking at a column position based on a column name and return the next value

Do you know how can I start looking for an specific text starting starting in a column name that has been given by the values found in another column?

Let me explain better with an example. The column 8 contains the column names where I have to start looking for the text 'office'. So, for the first row, the Col 8 indicates that I have to start at Col 2. Then I have to find the NEXT 'Office' text and return the value of the next column (always same row). Once I get it, I will create a DataFrame containing the next value, Col 4 in this example.

{'Col 1': {0: 3.4, 1: 4.6, 2: 7.6, 3: 3.7, 4: 5.9, 5: 2.5, 6: 2.6}, 
 'Col 2': {0: 'LTE', 1: 'LTE', 2: 'LTE', 3: 'LTE', 4: 'LTE', 5: 'LTE', 6: 'LTE'}, 
 'Col 3': {0: 'Office', 1: 'Office', 2: nan, 3: 'Office', 4: nan, 5: nan, 6: nan}, 
 'Col 4': {0: 1.2, 1: 3.1, 2: 23.0, 3: 11.0, 4: 34.0, 5: 12.0, 6: 123.0}, 
 'Col 5': {0: 'LTE', 1: 'LTE', 2: 'LTE', 3: 'LTE', 4: 'LTE', 5: 'LTE', 6: 'LTE'}, 
 'Col 6': {0: 'Office', 1: nan, 2: 'Office', 3: 'Office', 4: 'Office', 5: 'Office', 6: 'Office'}, 
 'Col 7': {0: 1.2, 1: 6.7, 2: 12.0, 3: 143.0, 4: 674.0, 5: 354.0, 6: 134.0}, 
 'Col 8': {0: 'Col 2', 1: 'Col 2', 2: 'Col 6', 3: 'Col 2', 4: 'Col 6', 5: 'Col 6', 6: 'Col 6'}}

any ideas on how to deal with this problem?

Output expected:

{'Col1': {0: '3.4', 1: '4.6', 2: '7.6', 3: '3.7', 4: '5.9', 5: '2.5', 6: '2.6'}, 
 'Col 4': {0: 1.2, 1: 3.1, 2: 12.0, 3: 11.0, 4: 674.0, 5: 354.0, 6: 134.0}}

which looks like:

  Col1  Col 4
0  3.4    1.2
1  4.6    3.1
2  7.6   12.0
3  3.7   11.0
4  5.9  674.0
5  2.5  354.0
6  2.6  134.0


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source