本文參考:
https://pythonexamples.org/pandas-dataframe-maximum-max/
import pandas as pd
mydictionary = {'physics': [68, 74, 77, 78],
'chemistry': [84, 56, 73, 69],
'algebra': [78, 88, 82, 87]}
# create dataframe
df_marks = pd.DataFrame(mydictionary)
print('DataFrame\n----------')
print(df_marks)
# calculate max of whole DataFrame
mean = df_marks.max().max()
print('\nMaximum Value\n------')
print(mean)
![Python: 如何求整個 pandas.DataFrame 中的最大值? pandas.DataFrame .max().max() ; 如何求最大值的index, columns? numpy.where(condition, [x, y, ]/) ; condition為一 bool_mask - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2023/04/20230418154049_50.png)
axis=0 , 1 :
![Python: 如何求整個 pandas.DataFrame 中的最大值? pandas.DataFrame .max().max() ; 如何求最大值的index, columns? numpy.where(condition, [x, y, ]/) ; condition為一 bool_mask - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2023/04/20230418154118_66.png)
推薦hahow線上學習python: https://igrape.net/30afN
import numpy as np
import pandas as pd
data = [[1, 2, 1], [4, 5, 4], [7, 8, 7]]
pPol_mean_Amp = pd.DataFrame(data, index=[0, 1, 2],
columns=[0, 1, 2])
max_row, max_col = np.where(pPol_mean_Amp ==
pPol_mean_Amp.values.max())
print("data:\n",data)
print("\nmax_row:",max_row, type(max_row))
print("\nmax_col:",max_col, type(max_col) )
“””pPol_mean_Amp.values 是 Numpy 陣列,
而非 Pandas.DataFrame,
所以在 Numpy 陣列上使用 .max() 方法可以直接找出最大值
不用.max().max()。
When only condition is provided,
this function is a shorthand(速記) for
np.asarray(condition).nonzero(). #不是nonzeros()
Using nonzero directly should be preferred,
as it behaves correctly for subclasses.
np.asarray() 和 np.array() 都可以用来将输入转换为numpy array。
不同之处在于,如果输入参数本身就是numpy array,
则np.asarray()不会进行副本操作,
而np.array()总是会进行副本操作。
例如,对于一个numpy array a,np.asarray(a) 返回的是 a 的视图,
而 np.array(a) 返回的是 a 的副本。 对于非numpy array 的输入,
则 np.asarray() 和 np.array() 的操作效果是一样的。
“””
![Python: 如何求整個 pandas.DataFrame 中的最大值? pandas.DataFrame .max().max() ; 如何求最大值的index, columns? numpy.where(condition, [x, y, ]/) ; condition為一 bool_mask - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2023/04/20230418162623_59.png)
max_row (max_col)是一個
1維,長度1的array
如果要取出數值
使用 max_row[0]
![Python: 如何求整個 pandas.DataFrame 中的最大值? pandas.DataFrame .max().max() ; 如何求最大值的index, columns? numpy.where(condition, [x, y, ]/) ; condition為一 bool_mask - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2023/04/20230418162817_59.png)
如果index/columns不是整數
而是-90~90, step=0.1 之類的浮點數
使用以下方法:
bool_mask = dfTA15_floatIdxCol == maxAmp
ary_idxr, ary_idxc = np.where(bool_mask)
# np.where() return Tuple[np.ndarray]
idxr = ary_idxr[0]
idxc = ary_idxc[0]
#idxr idxc是int64,表示第幾個,
#搭配.iloc[]定位也是可以
#但idxr_float可以直接看出來
#最大值發生的角度在那裡
idxr_float = df_floatIdxCol.index[idxr]
idxc_float = df_floatIdxCol.columns[idxc]
如果想要從np.where()
快進到idxr_float :
idxr_float = df_floatIdxCol.index[ np.where(bool_mask)[0][0] ]
會比較難看懂
驗證以下這句話
When only condition is provided,
this function is a shorthand(速記) for
np.asarray(condition).nonzero().
![Python: 如何求整個 pandas.DataFrame 中的最大值? pandas.DataFrame .max().max() ; 如何求最大值的index, columns? numpy.where(condition, [x, y, ]/) ; condition為一 bool_mask - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2023/04/20230418215955_16.png)
ary_mask = np.asarray(mask)
“””#取名為mask,是因為DataFrame[mask]
#可以起到遮罩的功能,
#即為官網所謂的condition
(condition: array_like, bool)
boolean array ,Series, DataFrame, list”””
![Python: 如何求整個 pandas.DataFrame 中的最大值? pandas.DataFrame .max().max() ; 如何求最大值的index, columns? numpy.where(condition, [x, y, ]/) ; condition為一 bool_mask - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2023/04/20230418220447_31.png)
mask.nonzero()
return一個tuple
tuple內容為非零元素的索引(type: ndarray)
所以mask.nonzero()
要取到索引數字(int)的話:
mask.nonzero()[0][0]
第一個[0]取tuple的第0個元素
本例也可能是[1]
第二個[0]取array的第0個元素
推薦hahow線上學習python: https://igrape.net/30afN
axis=0 ; axis=1
array跟DataFrame的value相同
只是DataFrame會多帶index:
![Python: 如何求整個 pandas.DataFrame 中的最大值? pandas.DataFrame .max().max() ; 如何求最大值的index, columns? numpy.where(condition, [x, y, ]/) ; condition為一 bool_mask - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2023/04/20230418211211_95.png)
但是ary.max()會返回ary中最大的元素
df.max()效果則同df.max(axis=0)
必須用df.max().max(),
才有跟ary.max()一樣的效果:
![Python: 如何求整個 pandas.DataFrame 中的最大值? pandas.DataFrame .max().max() ; 如何求最大值的index, columns? numpy.where(condition, [x, y, ]/) ; condition為一 bool_mask - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2023/04/20230418211324_35.png)
推薦hahow線上學習python: https://igrape.net/30afN

![Python Logging 完全指南:從基礎到實戰應用; import logging ; logging.basicConfig(level=logging.INFO, handlers=[ logging.StreamHandler(), logging.FileHandler(‘app.log’, mode=’a’, encoding=’utf-8′)] ) ; inspect.currentframe().f_code.co_name #動態取得funcName Python Logging 完全指南:從基礎到實戰應用; import logging ; logging.basicConfig(level=logging.INFO, handlers=[ logging.StreamHandler(), logging.FileHandler(‘app.log’, mode=’a’, encoding=’utf-8′)] ) ; inspect.currentframe().f_code.co_name #動態取得funcName](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2025/10/20251021155823_0_c16012.png?quality=90&zoom=2&ssl=1&resize=350%2C233)
![Python: 如何使用 os.environ[“PATH”] 設定環境變數?與 sys.path.append() 差別為何? Python: 如何使用 os.environ[“PATH”] 設定環境變數?與 sys.path.append() 差別為何?](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2024/09/20240905135312_0_890fa1.png?quality=90&zoom=2&ssl=1&resize=350%2C233)







近期留言