python中根據元素獲得索引

1. 獲取DataFrame的值的索引

(1)可以用DataFrame的條件索引,即令df_sub=df[conditions],然後再獲取df_sub的index屬性即可

如:random_fcd[random_fcd['time1']=='2.77'].index

(2)對於某一個列(series類型的數據),可以先轉化爲list類型,然後利用list.index(values)方法獲取索引,其中若有重複着,則返回第一個值對應的索引

 ##########

2. pandas中Series獲取最大值/最小值 及其對應的索引

問題:對於 pd.Series, 得到最大值的同時 也需要得到對應的索引(標籤)

示例:

d = [12, 4, 4, 8, 8, 2, 8, 10, 5, 4]
test = pd.Series(data=d)   #構造Series

## 獲取最大值/最小值 和對應的標籤 (方便在程序中使用)

test.max(), test.idxmax(),test.idxmin()
 

結果:
test.max(), test.idxmax(),test.idxmin()
Out[640]: (12, 0, 5)

 

3.  numpy中找到元素對應的索引

  • np.where(‘元素’)

 

4. list 中找到元素的索引

     list.index(value)

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章