pandas cut: how to convert categorical labels to strings (otherwise cannot export to Excel)?
Use astype(str)
:
writer = pd.ExcelWriter('test.xlsx')wk = writer.book.add_worksheet('Test')df= df= pd.DataFrame(np.random.randint(1,10,(10000,5)), columns=['a','b','c','d','e'])df['range'] = pd.cut( df['a'],[-np.inf,3,8,np.inf] ).astype(str)grouped=df.groupby('range').sum()grouped.to_excel(writer, 'Export')writer.close()
Output in excel:
range a b c d e(-inf, 3.0] 6798 17277 16979 17266 16949(3.0, 8.0] 33150 28051 27551 27692 27719(8.0, inf] 9513 5153 5318 5106 5412