'find the first occurrence of a value (from a list of values)in a pandas dataframe and return the index of the row
I have a pandas DataFrame (the actual data set is far larger and contains numbers from 1-50):
date main N1 N2 N3 N4 N5
0 2020-09-18 7-12-14-40-42 7 12 14 40 42
1 2020-09-11 2-5-24-43-45 2 5 24 43 45
2 2020-09-04 5-23-28-38-49 5 23 28 38 49
3 2020-08-28 8-11-22-38-41 8 11 22 38 41
4 2020-08-21 26-27-30-46-49 26 27 30 46 49
I want to return the row index for the first occurrence of each number. So far the only way that I have been able to achieve this is by "manually" using
lotteryData.loc[(lotteryData.N5==1)].head(49)
which in this example gives row index number 2. This is obviously very clumsy and does not take advantage of the Pandas DataFrame capability at all. Is there a way of iterating through the list in the format 5-23-28-38-49 directly to capture this information or a way to iterate through the columns to find the first occurrence of all numbers between 1 and 50
Solution 1:[1]
We can do stack the drop_duplicates
out = df.loc[:,'N2':].stack().drop_duplicates()
0 N2 12
N3 14
N4 40
N5 42
1 N2 5
N3 24
N4 43
N5 45
2 N2 23
N3 28
N4 38
N5 49
3 N2 11
N3 22
N5 41
4 N2 27
N3 30
N4 46
dtype: int64
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BENY |
