我正在尝试根据第二个数据帧中定义的 bin 将数据合并到一个数据帧中。我在想 pd.bin 和 pd.merge 的某种组合可能会让我到达那里?
这基本上是每个数据帧当前的形式:
df = pd.DataFrame({'id':['a', 'b', 'c', 'd','e'],
'bin':[1, 2, 3, 3, 2],
'perc':[0.1,0.9,0.3,0.7,0.5]})
df2 = pd.DataFrame({'bin':[1, 1, 1, 2, 2, 2, 3, 3, 3],
'result':['low', 'medium','high','low', 'medium','high','low', 'medium','high'],
'cut_min':[0,0.2,0.6,0,0.3,0.7,0,0.4,0.8],
'cut_max':[0.2,0.6,1,0.3,0.7,1,0.4,0.8,1]})
Run Code Online (Sandbox Code Playgroud)
df:
bin id perc
1 a 0.1
2 b 0.9
3 c 0.3
3 d 0.7
2 e 0.5
Run Code Online (Sandbox Code Playgroud)
这是带有垃圾箱的表格,df2:
bin cut_max cut_min result
1 0.2 0.0 low
1 0.6 0.2 medium
1 1.0 0.6 high
2 0.3 0.0 low
2 0.7 0.3 …Run Code Online (Sandbox Code Playgroud)