在Python中将已装箱的数据分组到不同的箱子中
我有特定箱子中提供的数据:在Python中将已装箱的数据分组到不同的箱子中,python,Python,我有特定箱子中提供的数据: 0 T<=-25 1619 -25<T<=-10 98391 -10<T<=0 698588 0<T<=15 782997 15<T<=20 我想将这些装箱值分配给新的箱子。新的边界是: T<-14 -14<T<=-12 -12<T<=-10 -10<T<=-8 -8<T<=-6 -6<T<=-4 -4&
0 T<=-25
1619 -25<T<=-10
98391 -10<T<=0
698588 0<T<=15
782997 15<T<=20
我想将这些装箱值分配给新的箱子。新的边界是:
T<-14
-14<T<=-12
-12<T<=-10
-10<T<=-8
-8<T<=-6
-6<T<=-4
-4<T<=-2
-2<T<=0
0<T<=2
2<T<=4
4<T<=6
6<T<=8
8<T<=10
10<T<=12
12<T<=14
14<T<=16
16<T<=18
18<T<=20
T>22
不确定这是否是实现你想要的最有效的方式,但以下是我通常做的:
def f(x):
# range T < -14
if x < -14:
# return value for corresponding bin
# range -14 < T < -12
if x < -12:
# return value for corresponding bin
# etc...
df['bin_value'] = df['T'].apply(f)
def(x):
#范围T<-14
如果x<-14:
#对应bin的返回值
#范围-14
您的问题和示例不清楚。
import pandas as pd
bb_temp = [-14 + i*2 for i in range( 0, 35 ) ]
d = {'val': [0, 1619, 98391, 698588, 782997, 308393, 131402, 16319, 899, 0, 0, 0, 0, 0 ], 'T_min': [ 0,-25,-10,0,10,20,25,30,35,40,45,50,55,60 ], 'T_max': [-25,-10,0,10,20,25,30,35,40,45,50,55,60,0 ]}
df = pd.DataFrame(data=d)
def f(row):
bb = [ i for i in bb_temp if i > row[ "T_min" ] and i <= row[ "T_max" ] ]
if len( bb ) == 0:
bb = [0]
new_bin_val = row[ "val" ] / len(bb)
return ( bb, new_bin_val )
df[ 'new_bin' ] = df.apply( f, axis=1 )
df
val T_min T_max new_bin
0 0 0 -25 ([0], 0.0)
1 1619 -25 -10 ([-14, -12, -10], 539.6666666666666)
2 98391 -10 0 ([-8, -6, -4, -2, 0], 19678.2)
3 698588 0 10 ([2, 4, 6, 8, 10], 139717.6)
4 782997 10 20 ([12, 14, 16, 18, 20], 156599.4)
5 308393 20 25 ([22, 24], 154196.5)
6 131402 25 30 ([26, 28, 30], 43800.666666666664)
7 16319 30 35 ([32, 34], 8159.5)
8 899 35 40 ([36, 38, 40], 299.6666666666667)
9 0 40 45 ([42, 44], 0.0)
10 0 45 50 ([46, 48, 50], 0.0)
11 0 50 55 ([52, 54], 0.0)
12 0 55 60 ([0], 0.0)
13 0 60 0 ([0], 0.0)
def f(x):
# range T < -14
if x < -14:
# return value for corresponding bin
# range -14 < T < -12
if x < -12:
# return value for corresponding bin
# etc...
df['bin_value'] = df['T'].apply(f)