У меня часто бывают ситуации, когда мне нужно найти первый экземпляр значения в таблице. Например, ниже я должен найти цвет первого экземпляра каждого типа candy_type, упорядоченного по последовательности:
d = {'candy_type':['A','A','B','B','C','C','C'],'sequence':[2,1,1,2,2,3,1], 'color':['Red','Black','Green','Yellow','Orange','White','Purple']}
df = pd.DataFrame(data=d)
df
+----+--------------+------------+---------+
| | candy_type | sequence | color |
|----+--------------+------------+---------|
| 0 | A | 2 | Red |
| 1 | A | 1 | Black |
| 2 | B | 1 | Green |
| 3 | B | 2 | Yellow |
| 4 | C | 2 | Orange |
| 5 | C | 3 | White |
| 6 | C | 1 | Purple |
+----+--------------+------------+---------+
#sort the dataframe by each candy_type's sequence and reset the index
df_sorted = df.sort_values(['candy_type','sequence']).reset_index(drop=True)
#make the index into a column
df_sorted_index = df_sorted.reset_index(drop=False)
df_sorted_index
+----+---------+--------------+------------+---------+
| | index | candy_type | sequence | color |
|----+---------+--------------+------------+---------|
| 0 | 0 | A | 1 | Black |
| 1 | 1 | A | 2 | Red |
| 2 | 2 | B | 1 | Green |
| 3 | 3 | B | 2 | Yellow |
| 4 | 4 | C | 1 | Purple |
| 5 | 5 | C | 2 | Orange |
| 6 | 6 | C | 3 | White |
+----+---------+--------------+------------+---------+
#find the first instance of each candy type; show the whole row
df_sorted_index.loc[df_sorted_index.groupby('candy_type')['index'].idxmin()]
+----+---------+--------------+------------+---------+
| | index | candy_type | sequence | color |
|----+---------+--------------+------------+---------|
| 0 | 0 | A | 1 | Black |
| 2 | 2 | B | 1 | Green |
| 4 | 4 | C | 1 | Purple |
+----+---------+--------------+------------+---------+