python的等价物是什么;R中的idxmin()是什么?
我经常遇到这样的情况:我必须在表中找到值的第一个实例。例如,下面我必须找到按顺序排序的每个candy_类型的第一个实例的颜色:python的等价物是什么;R中的idxmin()是什么?,r,R,我经常遇到这样的情况:我必须在表中找到值的第一个实例。例如,下面我必须找到按顺序排序的每个candy_类型的第一个实例的颜色: 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 +----+-------
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 |
+----+---------+--------------+------------+---------+
您可以使用
匹配:
##创建排序数据.frame
d您可以使用match
:
##创建排序数据.frame
d哪个.min()
是R的等价物idxmin()。两者都在数组中查找最小值,并返回第一个这样的值的索引-如果存在关联,则非常有用。哪个.min()是R的等价项idxmin()。两者都在数组中查找最小值并返回第一个这样的值的索引—如果存在关系,则非常有用