我有一个数据集,其中“类型”列基本上是形状,与此相对应,“体积”列由该形状的体积组成
现在我需要执行以下任务:
- 按形状分组
- 对于每个形状,按体积分组
- 对于每个形状和体积,定义一个范围并形成箱
输入:
Type Volume
Cylinder 100
Square 300
Cylinder 200
Oval 100
Square 320
Cylinder 150
Oval 600
Round 1000
Square 900
Round 1500
输出:
Type Volume Bin
Cylinder 100 1
Cylinder 150 1
Cylinder 200 2
Oval 100 1
Oval 600 3
Round 1000 1
Round 1500 2
Square 300 1
Square 320 1
Square 900 3
垃圾箱如下:
1.Cylinder -> Bin1(100-200), Bin2(201-300) ....
2.椭圆 -> Bin1(100-200), ..... Bin3(500-600).... ...
代码:
grouped=df_dim.groupby('Type', as_index=False)
def test(group):
return group.reset_index()
def group_vol(group):
groupedVol =
group.groupby(pd.cut(group["Target_BrimVol"],
np.arange(0,5000,200)),as_index=False)
return groupedVol.apply(test)
gr = grouped.apply(group_vol)
print(gr)