Python是進行數據分析的一種出色語言,主要是因為以數據為中心的python軟件包具有奇妙的生態係統。 Pandas是其中的一種,使導入和分析數據更加容易。
Pandas dataframe.idxmax()
函數返回在請求的軸上第一次出現最大值的索引。在任何索引中找到最大值的索引時,將排除所有NA /空值。
用法: DataFrame.idxmax(axis=0, skipna=True)
參數:
axis:0或“索引”表示行,1或“列”表示列
skipna:排除NA /空值。如果整個行/列均為NA,則結果為NA
返回:idxmax:係列
範例1:采用idxmax()
函數用於沿索引軸查找最大值的索引。
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.DataFrame({"A":[4, 5, 2, 6],
"B":[11, 2, 5, 8],
"C":[1, 8, 66, 4]})
# Print the dataframe
df
現在應用idxmax()
沿索引軸起作用。
# applying idxmax() function.
df.idxmax(axis = 0)
輸出:
如果我們查看 DataFrame 中的值,則可以驗證該函數返回的結果。該函數返回一個 Pandas 係列對象,該對象在每一列中均包含最大值索引。
範例2:采用idxmax()
函數沿列軸查找最大值的索引。 DataFrame 包含NA
值。
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.DataFrame({"A":[4, 5, 2, None],
"B":[11, 2, None, 8],
"C":[1, 8, 66, 4]})
# Skipna = True will skip all the Na values
# find maximum along column axis
df.idxmax(axis = 1, skipna = True)
輸出:
輸出是一個 Pandas 係列,其中包含具有最大值的每一行的列標簽。
相關用法
- Python pandas.map()用法及代碼示例
- Python Pandas Series.str.len()用法及代碼示例
- Python Pandas.factorize()用法及代碼示例
- Python Pandas TimedeltaIndex.name用法及代碼示例
- Python Pandas dataframe.ne()用法及代碼示例
- Python Pandas Series.between()用法及代碼示例
- Python Pandas DataFrame.where()用法及代碼示例
- Python Pandas Series.add()用法及代碼示例
- Python Pandas.pivot_table()用法及代碼示例
- Python Pandas Series.mod()用法及代碼示例
- Python Pandas Dataframe.at[ ]用法及代碼示例
- Python Pandas Dataframe.iat[ ]用法及代碼示例
- Python Pandas.pivot()用法及代碼示例
- Python Pandas dataframe.mul()用法及代碼示例
- Python Pandas.melt()用法及代碼示例
注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas dataframe.idxmax()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。