Q1: 在numpy的argmax (argmin)指令中,如果陣列最大值(或最小值)的數字出現在兩個以上的元素,指令只會抓出其中index最小的位置。如果想要抓出所有最大值的位置,是不是有別的指令?
A = np.array([1,2,3,1])
indminA = np.argmin(A) ← smallest index of where minimum of A occur
A1: 先找出最大/小值,用 (A==…).nonzero()找出與最大(小)值相等的所有位置:
minA = np.amin(A)
all_indminA = (A==minA).nonzero()
all_indminA[:] ← indices of where minimum of A occur

Series也類似:

使用isin():

要取得元素 == True的index
原本要寫好幾行:

現在只要一行:
boolAry.nonzero()
輸出: (array([0, 3], dtype=int64),)
需要注意,其為長度1的tuple
boolAry.nonzero()[0] 才是array
pandas如何用 .iloc[bool_list] 取出判斷式為真的那一列?
pandas.Series.str.contains(“Hz”)
處理DataFrame常會用到bool_list
需要取得元素==True的index
可用.nonzero()替代
boolAry:

想取出元素(==True的index)有點特別
boolAry.nonzero():

推薦hahow線上學習python: https://igrape.net/30afN


![Python 表達式中的魔法:用海象運算子讓斷詞程式碼更乾淨 [w_clean for w in words if (w_clean:=w.lower().strip()) and w_clean not in STOPWORDS] Python 表達式中的魔法:用海象運算子讓斷詞程式碼更乾淨 [w_clean for w in words if (w_clean:=w.lower().strip()) and w_clean not in STOPWORDS]](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2026/02/20260210083748_0_a7d9bf.png?quality=90&zoom=2&ssl=1&resize=350%2C233)



![Python: pandas.DataFrame 如何對某些欄做格式化字串? apply(); applymap() ; map() 的差別? df[‘Salary’] = df[‘Salary’].map( ‘${:,.2f}’ .format) Python: pandas.DataFrame 如何對某些欄做格式化字串? apply(); applymap() ; map() 的差別? df[‘Salary’] = df[‘Salary’].map( ‘${:,.2f}’ .format)](https://i1.wp.com/savingking.com.tw/wp-content/uploads/2023/05/20230527091636_49.png?quality=90&zoom=2&ssl=1&resize=350%2C233)



近期留言