Python - Pandas 数据框中的分组列
要对 Pandas 数据框中的列进行分组,请使用 groupby()。首先,让我们创建一个 Pandas 数据框 -
dataFrame = pd.DataFrame( { "Car": ["Audi", "Lexus", "Audi", "Mercedes", "Audi", "Lexus", "Mercedes", "Lexus", "Mercedes"], "Reg_Price": [1000, 1400, 1100, 900, 1700, 1800, 1300, 1150, 1350] } )
现在,让我们按照 Car 列进行分组 -
res = dataFrame.groupby("Car")
分组后,我们将使用函数来查找分组车辆名称的平均注册价格 (Reg_Price) -
res.mean()
这会根据 Car 列计算注册价格的平均值。
示例
以下是代码 -
import pandas as pd # dataframe with one of the columns as Reg_Price dataFrame = pd.DataFrame( { "Car": ["Audi", "Lexus", "Audi", "Mercedes", "Audi", "Lexus", "Mercedes", "Lexus", "Mercedes"], "Reg_Price": [1000, 1400, 1100, 900, 1700, 1800, 1300, 1150, 1350] } ) print"DataFrame...\n",dataFrame # grouped according to Car res = dataFrame.groupby("Car") print"\nMean of Registration Price grouped according to Car names...\n",res.mean()
输出
这将生成以下输出 -
DataFrame... Car Reg_Price 0 Audi 1000 1 Lexus 1400 2 Audi 1100 3 Mercedes 900 4 Audi 1700 5 Lexus 1800 6 Mercedes 1300 7 Lexus 1150 8 Mercedes 1350 Mean of Registration Price grouped according to Car names... Reg_Price Car Audi 1266.666667 Lexus 1450.000000 Mercedes 1183.333333
广告