Python——计算 Pandas DataFrame 的最大列值
若要获取最大列值,请使用 max() 函数。首先,导入必需的 Pandas 库 −
import pandas as pd
现在,创建含两列的 DataFrame −
dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'],"Units": [100, 150, 110, 80, 110, 90] } )
使用 max() 查找单个列 “Units” 的最大值 −
print"Maximum Units from DataFrame1 = ",dataFrame1['Units'].max()
同理,我们从第 2 个 DataFrame 计算了最大值。
示例
以下为完整代码 −
import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'],"Units": [100, 150, 110, 80, 110, 90] } ) print"DataFrame1 ...\n",dataFrame1 # Finding maximum value of a single column "Units" print"Maximum Units from DataFrame1 = ",dataFrame1['Units'].max() # Create DataFrame2 dataFrame2 = pd.DataFrame( { "Product": ['TV', 'PenDrive', 'HeadPhone', 'EarPhone', 'HDD', 'SSD'],"Price": [8000, 500, 3000, 1500, 3000, 4000] } ) print"\nDataFrame2 ...\n",dataFrame2 # Finding maximum value of a single column "Price" print"Maximum Price from DataFrame2 = ",dataFrame2['Price'].max()
输出
这将产生以下输出 −
DataFrame1 ... Car Units 0 BMW 100 1 Lexus 150 2 Audi 110 3 Tesla 80 4 Bentley 110 5 Jaguar 90 Maximum Units from DataFrame1 = 150 DataFrame2 ... Price Product 0 8000 TV 1 500 PenDrive 2 3000 HeadPhone 3 1500 EarPhone 4 3000 HDD 5 4000 SSD Maximum Price from DataFrame2 = 8000
广告