Apologies for the wording of the question but I can illustrate my problem more clearly below.
Suppose I have the following dataframe: