Python 将df.groupby()形成的组拆分为具有这些分组值的多个数据帧
我有一个包含列的数据集,如Python 将df.groupby()形成的组拆分为具有这些分组值的多个数据帧,python,pandas,dataframe,group-by,pandas-groupby,Python,Pandas,Dataframe,Group By,Pandas Groupby,我有一个包含列的数据集,如变速箱、传动系和燃油类型,这三个列都是分类的。现在,如果我运行代码: df.groupby(['Transmission','dt','Fuel_Type'])['PRICE'].mean() 它给出了多个组及其平均价格。我想要的是假设这个groupby给我这样的输出(5组): 因此,现在我需要5个新的数据帧,分别带有变速箱、传动系和燃油类型值以及其他条目和列。您可以执行以下操作。假设您的数据帧如下所示: Transmission drivetrain
变速箱
、传动系
和燃油类型
,这三个列都是分类的。现在,如果我运行代码:
df.groupby(['Transmission','dt','Fuel_Type'])['PRICE'].mean()
它给出了多个组及其平均价格。我想要的是假设这个groupby给我这样的输出(5组):
因此,现在我需要5个新的数据帧,分别带有
变速箱
、传动系
和燃油类型
值以及其他条目和列。您可以执行以下操作。假设您的数据帧如下所示:
Transmission drivetrain Fuel_type PRICE
0 Automatic All-Wheel Drive Diesel 434344
1 Manual Four-Wheel Drive Diesel 323232
2 CVT All-Wheel Drive Diesel 5355
3 Automatic Four-Wheel Drive Diesel 53534534
4 Manual Four-Wheel Drive Gasoline 4424422
5 CVT All-Wheel Drive Gasoline 242442
6 Automatic Four-Wheel Drive Gasoline 42424
7 Manual Four-Wheel Drive Gasoline 57757
8 CVT Four-Wheel Drive Gasoline 4244
9 Automatic Four-Wheel Drive Flex 78997
10 Manual All-Wheel Drive Flex 7989
11 CVT Four-Wheel Drive Flex 46456
12 Automatic Four-Wheel Drive Flex 4646
13 Manual All-Wheel Drive Flex 4646
14 CVT Four-Wheel Drive Flex 487979
现在,您需要按组计算平均值:
df.groupby(['Transmission', 'drivetrain', 'Fuel_type'])['PRICE'].mean()
其中打印:
Transmission drivetrain Fuel_type
Automatic All-Wheel Drive Diesel 434344.0
Four-Wheel Drive Diesel 53534534.0
Flex 41821.5
Gasoline 42424.0
CVT All-Wheel Drive Diesel 5355.0
Gasoline 242442.0
Four-Wheel Drive Flex 267217.5
Gasoline 4244.0
Manual All-Wheel Drive Flex 6317.5
Four-Wheel Drive Diesel 323232.0
Gasoline 2241089.5
Name: PRICE, dtype: float64
现在,为了能够为每个组创建单独的数据帧:
(Automatic, Four-Wheel Drive, Diesel, Mean)
((Automatic, All-Wheel Drive, Diesel, Mean)
......
可以将每个组合转换为字典中的元素:
Groups = dict(tuple(df.groupby(['Transmission','drivetrain','Fuel_type'])))
Groups
其中:
{('Automatic',
'All-Wheel Drive',
'Diesel'): Transmission drivetrain Fuel_type PRICE
0 Automatic All-Wheel Drive Diesel 434344,
('Automatic',
'Four-Wheel Drive',
'Diesel'): Transmission drivetrain Fuel_type PRICE
3 Automatic Four-Wheel Drive Diesel 53534534,
('Automatic',
'Four-Wheel Drive',
'Flex'): Transmission drivetrain Fuel_type PRICE
9 Automatic Four-Wheel Drive Flex 78997
12 Automatic Four-Wheel Drive Flex 4646,
('Automatic',
'Four-Wheel Drive',
'Gasoline'): Transmission drivetrain Fuel_type PRICE
6 Automatic Four-Wheel Drive Gasoline 42424,
('CVT',
'All-Wheel Drive',
'Diesel'): Transmission drivetrain Fuel_type PRICE
2 CVT All-Wheel Drive Diesel 5355,
('CVT',
'All-Wheel Drive',
'Gasoline'): Transmission drivetrain Fuel_type PRICE
5 CVT All-Wheel Drive Gasoline 242442,
('CVT',
'Four-Wheel Drive',
'Flex'): Transmission drivetrain Fuel_type PRICE
11 CVT Four-Wheel Drive Flex 46456
14 CVT Four-Wheel Drive Flex 487979,
('CVT',
'Four-Wheel Drive',
'Gasoline'): Transmission drivetrain Fuel_type PRICE
8 CVT Four-Wheel Drive Gasoline 4244,
('Manual',
'All-Wheel Drive',
'Flex'): Transmission drivetrain Fuel_type PRICE
10 Manual All-Wheel Drive Flex 7989
13 Manual All-Wheel Drive Flex 4646,
('Manual',
'Four-Wheel Drive',
'Diesel'): Transmission drivetrain Fuel_type PRICE
1 Manual Four-Wheel Drive Diesel 323232,
('Manual',
'Four-Wheel Drive',
'Gasoline'): Transmission drivetrain Fuel_type PRICE
4 Manual Four-Wheel Drive Gasoline 4424422
7 Manual Four-Wheel Drive Gasoline 57757}
Transmission drivetrain Fuel_type PRICE
0 Automatic All-Wheel Drive Diesel 434344
从中,您可以提取所需的任何组合,例如:
df_aut_drive_fuel = Groups['Automatic','All-Wheel Drive', 'Diesel']
其中:
{('Automatic',
'All-Wheel Drive',
'Diesel'): Transmission drivetrain Fuel_type PRICE
0 Automatic All-Wheel Drive Diesel 434344,
('Automatic',
'Four-Wheel Drive',
'Diesel'): Transmission drivetrain Fuel_type PRICE
3 Automatic Four-Wheel Drive Diesel 53534534,
('Automatic',
'Four-Wheel Drive',
'Flex'): Transmission drivetrain Fuel_type PRICE
9 Automatic Four-Wheel Drive Flex 78997
12 Automatic Four-Wheel Drive Flex 4646,
('Automatic',
'Four-Wheel Drive',
'Gasoline'): Transmission drivetrain Fuel_type PRICE
6 Automatic Four-Wheel Drive Gasoline 42424,
('CVT',
'All-Wheel Drive',
'Diesel'): Transmission drivetrain Fuel_type PRICE
2 CVT All-Wheel Drive Diesel 5355,
('CVT',
'All-Wheel Drive',
'Gasoline'): Transmission drivetrain Fuel_type PRICE
5 CVT All-Wheel Drive Gasoline 242442,
('CVT',
'Four-Wheel Drive',
'Flex'): Transmission drivetrain Fuel_type PRICE
11 CVT Four-Wheel Drive Flex 46456
14 CVT Four-Wheel Drive Flex 487979,
('CVT',
'Four-Wheel Drive',
'Gasoline'): Transmission drivetrain Fuel_type PRICE
8 CVT Four-Wheel Drive Gasoline 4244,
('Manual',
'All-Wheel Drive',
'Flex'): Transmission drivetrain Fuel_type PRICE
10 Manual All-Wheel Drive Flex 7989
13 Manual All-Wheel Drive Flex 4646,
('Manual',
'Four-Wheel Drive',
'Diesel'): Transmission drivetrain Fuel_type PRICE
1 Manual Four-Wheel Drive Diesel 323232,
('Manual',
'Four-Wheel Drive',
'Gasoline'): Transmission drivetrain Fuel_type PRICE
4 Manual Four-Wheel Drive Gasoline 4424422
7 Manual Four-Wheel Drive Gasoline 57757}
Transmission drivetrain Fuel_type PRICE
0 Automatic All-Wheel Drive Diesel 434344
或
返回:
Transmission drivetrain Fuel_type PRICE
4 Manual Four-Wheel Drive Gasoline 4424422
7 Manual Four-Wheel Drive Gasoline 57757
你好你能分享一份数据样本吗?可能会有帮助。还有,这可能是你想要的答案:是的,兄弟,它工作得很快。谢谢!很高兴它有帮助!