I am iterating through a column in multiple dataframes df1['values']
, df2['values']
, and so on. The elements in ['values']
look like this:
[3, 1, 3, 1, 1, 4, 4, 3, 3, 1, 2, 1, 1, 3, 4, 2, 3, 1, 1, 1]
I want to loop through the column and append the first occurrence of 4 to a new list and break the loop. However, if 3 occurs before 4, then I want to append that 1st occurrence of 3, ignore any subsequent occurrence of 3, but continue looping until I get to the first occurrence of 4, then break the loop.
So, with the list above:
[3, 1, 3, 1, 1, 4, 4, 3, 3, 1, 2, 1, 1, 3, 4, 2, 3, 1, 1, 1]
Desired output would be: [3,4]
But if the list looked like: [2, 1, 2, 1, 1, 4, 4, 3, 3, 1, 2, 1, 1, 3, 4, 2, 3, 1, 1, 1]
Desired output would be: [4]
CodePudding user response:
You could do it like this:
u = df['values'].unique()
u = u[u >= 3]
u = u[:(u == 4).argmax() 1]
Output (using first list):
>>> u
array([3, 4])
Output (using second list):
>>> u
array([4])