如何在Python中从Pandas数据框中获取最大值
Python Pandas max()函数,返回所请求的轴上数值的最大值。
语法 : dataframe.max(axis)
其中,
- axis=0指定列
- axis=1指定行
例1:获取数据框架行的最大值
要获得数据框架行中的最大值,只需调用max()函数,轴设置为1。
语法 : dataframe.max(axis=1)
# import pandas module
import pandas as pd
# create a dataframe
# with 5 rows and 4 columns
data = pd.DataFrame({
'name': ['sravan', 'ojsawi', 'bobby',
'rohith', 'gnanesh'],
'subjects': ['java', 'php', 'html/css',
'python', 'R'],
'marks': [98, 90, 78, 91, 87],
'age': [11, 23, 23, 21, 21]
})
# display dataframe
print(data)
# get the maximum in row
data.max(axis=1)
输出:
例2:获取列中的最大值
要获得一列中的最大值,只需使用设置为0的轴调用max()函数。
语法 : dataframe.max(axis=0)
# import pandas module
import pandas as pd
# create a dataframe
# with 5 rows and 4 columns
data = pd.DataFrame({
'name': ['sravan', 'ojsawi', 'bobby',
'rohith', 'gnanesh'],
'subjects': ['java', 'php', 'html/css',
'python', 'R'],
'marks': [98, 90, 78, 91, 87],
'age': [11, 23, 23, 21, 21]
})
# display dataframe
print(data)
# get the maximum in column
data.max(axis=0)
输出:
例3:获取某一列中的最大值
要想获得某一列的最大值,可以用特定的列名和max()函数调用数据框架。
语法 : dataframe[‘column_name’].max()
# import pandas module
import pandas as pd
# create a dataframe
# with 5 rows and 4 columns
data = pd.DataFrame({
'name': ['sravan', 'ojsawi', 'bobby',
'rohith', 'gnanesh'],
'subjects': ['java', 'php', 'html/css',
'python', 'R'],
'marks': [98, 90, 78, 91, 87],
'age': [11, 23, 23, 21, 21]
})
# display dataframe
print(data)
# get the max in name column
print(data['name'].max())
# get the max in subjects column
print(data['subjects'].max())
# get the max in age column
print(data['age'].max())
# get the max in marks column
print(data['marks'].max())
输出: