Python - 计算 Pandas DataFrame 中列值的最小值
若要获取列值的最小值,请使用 min() 函数。首先,导入所需的 Pandas 库 -
import pandas as pd
现在,创建一个包含两列的 DataFrame -
dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'],"Units": [100, 150, 110, 80, 110, 90] } )
使用 min() 查找“Units”这一列的最小值 -
print"Minimum Units from DataFrame1 = ",dataFrame1['Units'].min()
使用相同的方式,我们从第二个 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 minimum value of a single column "Units" print"Minimum Units from DataFrame1 = ",dataFrame1['Units'].min() # Create DataFrame2 dataFrame2 = pd.DataFrame( { "Product": ['TV', 'PenDrive', 'HeadPhone', 'EarPhone', 'HDD', 'SSD'],"Price": [8000, 500, 3000, 1500, 3000, 4000] } ) print"\nDataFrame2 ...\n",dataFrame2 # Finding minimum value of a single column "Price" print"Minimum Price from DataFrame2 = ",dataFrame2['Price'].min()
输出
以下为运行输出 -
DataFrame1 ... Car Units 0 BMW 100 1 Lexus 150 2 Audi 110 3 Tesla 80 4 Bentley 110 5 Jaguar 90 Minimum Units from DataFrame1 = 80 DataFrame2 ... Price Product 0 8000 TV 1 500 PenDrive 2 3000 HeadPhone 3 1500 EarPhone 4 3000 HDD 5 4000 SSD Minimum Price from DataFrame2 = 500
广告