我正在PANDAs中创建一个数据透视表,并尝试对使用pd.cut创建的分类类型的bin进行排序。但是他们按照这样的顺序排序...... [1,3),[20,25),[3,8),[30,35),[8,20)
这是我的代码。我想也许我可以使用表示“Bin 1 _...”,“Bin2 _...”等字符串来格式化bin标签...
df['tpb'] = df['tpb'].astype('float64')
bins2 = [0,1,2,4,8,15,25,50]
diff=np.diff(bins2).tolist()
joint = list(zip(bins2,diff))
s1 = "{left:,.0f} to {right:,.0f}"
labels2 = [s1.format(left=yr[0], right=yr[0]+yr[1]-1) for yr in joint]
df['tpb_bin'] = pd.cut(df['tpb'],bins2,right=False,labels=labels2)
table = pd.pivot_table(df,index=['category','day','tpb_bin'],columns=['test_type'])