【记录】创建DataFrame索引并取值
a = np.arange(1, 25).reshape(8, 3) print(a) #[[ 1 2 3] # [ 4 5 6] # [ 7 8 9] # [10 11 12] # [13 14 15] # [16 17 18] # [19 20 21] # [22 23 24]] name = 'month' name_year = 'year' month_index = [] month_index += [name + '%d' % (month + 1) for month in range(8)] print(month_index) #['month1', 'month2', 'month3', 'month4', 'month5', 'month6', 'month7', 'month8'] year_index = [] year_index += [name_year + '%d' % (year + 1) for year in range(3)] print(year_index) #['year1', 'year2', 'year3'] df = pd.DataFrame(a, index=month_index, columns=year_index) print(df) # year1 year2 year3 #month1 1 2 3 #month2 4 5 6 #month3 7 8 9 #month4 10 11 12 #month5 13 14 15 #month6 16 17 18 #month7 19 20 21 #month8 22 23 24 print(df.loc[['month1', 'month2']][['year1', 'year2']]) # year1 year2 #month1 1 2 #month2 4 5 print(df.loc[['month1', 'month2']][year_index[0:2]]) # year1 year2 #month1 1 2 #month2 4 5 print(df.loc[month_index[0:2]][year_index[0:2]]) # year1 year2 #month1 1 2 #month2 4 5 # 输出DataFrame信息至excel write = pd.ExcelWriter('D:\\数据.xlsx') df.to_excel(write) write.save()参考文章 【1】https://blog.csdn.net/houyanhua1/article/details/87874397 【2】https://blog.csdn.net/maliang_1993/article/details/50907983?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param