1.设置多级索引:
res1 = pd.DataFrame(np.zeros((32,8), dtype=np.float),index=index_lis,columns=[[2021+i, 2021+i, 2021+i, 2021+i, 2021+i, 2021+i, 2021+i, 2021+i],
["参保人口","参保人口","受益人口","受益人口","受益人口","受益人口","受益人口","受益人口"],
["男", "女", "老人男", "老人女", "中人男", "中人女", "新人男", "新人女"]])
设置后的索引输出excel如图所示:
2.插入行:
目前没有发现可以直接插入行的方法,所以就直接定义新的行,然后用再用切片来调整位置
插入语句:df.loc[索引名] = [行内容]
3.pandas输出为Excel:
writer = pd.ExcelWriter('myExcel.xlsx')
df.to_excel(writer,float_format='%.5f')
writer.save()