如何在 Pandas 中使用 Groupby 函数按年分组时查找不同 类 的百分比?
How to find percentages of different classes while grouping by year using Groupby function in Pandas?
所以我有一个来自 Kaggle 的 used Audi car database。
这是我导入数据集的代码:
### headers ###
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
### path name ###
data_file_pathname = "etc.."
### import ###
my_data_frame = pd.read_csv(data_file_pathname, sep=",", header=0)
### preview of dataset ###
print(my_data_frame.head())
model year price transmission mileage fuelType tax mpg engineSize
0 A1 2017 12500 Manual 15735 Petrol 150 55.4 1.4
1 A6 2016 16500 Automatic 36203 Diesel 20 64.2 2.0
2 A1 2016 11000 Manual 29946 Petrol 30 55.4 1.4
3 A4 2017 16800 Automatic 25952 Diesel 145 67.3 2.0
4 A3 2019 17300 Manual 1998 Petrol 145 49.6 1.0
年份从 1997 年到 2021 年,我有 3 种燃料类型(汽油、柴油和混合动力)。
我想要的是每年燃料类型百分比的数据集。
示例:
year fuel percentage
2003 Petrol 20.00
Diesel 65.00
Hybrid 15.00
到目前为止,我已经设法获得了每年每种燃料类型的汽车数量,但我什至不确定我是怎么做到的...这是相关代码:
Fuel_Year = my_data_frame.groupby(['year', 'fuelType'])
df = pd.concat([Fuel_Year.fuelType.count()], axis=1, keys="Counts")
print(df)
C
year fuelType
1997 Petrol 1
1998 Petrol 1
2002 Petrol 2
2003 Diesel 4
Petrol 2
2004 Diesel 1
Petrol 4
2005 Diesel 2
Petrol 5
2006 Diesel 3
Petrol 6
2007 Diesel 7
Petrol 9
我尝试了不同的方法,并在我检查过的许多网页上使用了 transform('sum') 函数,但这对我来说并不成功。我的最终目标是创建某种堆叠直方图,其中 x 轴为年份,y 轴为百分比,以显示每年燃料类型的演变。
谁能帮我找到正确的代码来生成每年的燃料类型百分比列?
非常感谢! :)
df.groupby(['year'])['fuelType'].value_counts(normalize=True) * 100
year fuelType
2016 Diesel 50.0
Petrol 50.0
2017 Diesel 50.0
Petrol 50.0
2019 Petrol 100.0
Name: fuelType, dtype: float64
为此,您可以利用 pandas 数据框的 value_counts() 函数。它在很多情况下对你来说都很方便。
round((df.groupby(['year'])['fuelType'].value_counts()/df.groupby('year')['fuelType'].count()) * 100,2)
year fuelType
1997 Petrol 100.00
1998 Petrol 100.00
2002 Petrol 100.00
2003 Diesel 66.67
Petrol 33.33
2004 Petrol 80.00
Diesel 20.00
2005 Petrol 71.43
Diesel 28.57
2006 Petrol 66.67
Diesel 33.33
如有任何疑问,请告诉我。干杯!
所以我有一个来自 Kaggle 的 used Audi car database。
这是我导入数据集的代码:
### headers ###
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
### path name ###
data_file_pathname = "etc.."
### import ###
my_data_frame = pd.read_csv(data_file_pathname, sep=",", header=0)
### preview of dataset ###
print(my_data_frame.head())
model year price transmission mileage fuelType tax mpg engineSize
0 A1 2017 12500 Manual 15735 Petrol 150 55.4 1.4
1 A6 2016 16500 Automatic 36203 Diesel 20 64.2 2.0
2 A1 2016 11000 Manual 29946 Petrol 30 55.4 1.4
3 A4 2017 16800 Automatic 25952 Diesel 145 67.3 2.0
4 A3 2019 17300 Manual 1998 Petrol 145 49.6 1.0
年份从 1997 年到 2021 年,我有 3 种燃料类型(汽油、柴油和混合动力)。 我想要的是每年燃料类型百分比的数据集。
示例:
year fuel percentage
2003 Petrol 20.00
Diesel 65.00
Hybrid 15.00
到目前为止,我已经设法获得了每年每种燃料类型的汽车数量,但我什至不确定我是怎么做到的...这是相关代码:
Fuel_Year = my_data_frame.groupby(['year', 'fuelType'])
df = pd.concat([Fuel_Year.fuelType.count()], axis=1, keys="Counts")
print(df)
C
year fuelType
1997 Petrol 1
1998 Petrol 1
2002 Petrol 2
2003 Diesel 4
Petrol 2
2004 Diesel 1
Petrol 4
2005 Diesel 2
Petrol 5
2006 Diesel 3
Petrol 6
2007 Diesel 7
Petrol 9
我尝试了不同的方法,并在我检查过的许多网页上使用了 transform('sum') 函数,但这对我来说并不成功。我的最终目标是创建某种堆叠直方图,其中 x 轴为年份,y 轴为百分比,以显示每年燃料类型的演变。
谁能帮我找到正确的代码来生成每年的燃料类型百分比列?
非常感谢! :)
df.groupby(['year'])['fuelType'].value_counts(normalize=True) * 100
year fuelType
2016 Diesel 50.0
Petrol 50.0
2017 Diesel 50.0
Petrol 50.0
2019 Petrol 100.0
Name: fuelType, dtype: float64
为此,您可以利用 pandas 数据框的 value_counts() 函数。它在很多情况下对你来说都很方便。
round((df.groupby(['year'])['fuelType'].value_counts()/df.groupby('year')['fuelType'].count()) * 100,2)
year fuelType
1997 Petrol 100.00
1998 Petrol 100.00
2002 Petrol 100.00
2003 Diesel 66.67
Petrol 33.33
2004 Petrol 80.00
Diesel 20.00
2005 Petrol 71.43
Diesel 28.57
2006 Petrol 66.67
Diesel 33.33
如有任何疑问,请告诉我。干杯!