1. 程式人生 > 其它 >11.pandas獲取EXCEL某一行某一列資料,組合成二維陣列,再寫入excel

11.pandas獲取EXCEL某一行某一列資料,組合成二維陣列,再寫入excel

 1     def openxls(self):
 2         '''獲取第一個sheet的資料'''
 3         df = pd.read_excel(self.filepath, sheet_name=0)
 4         '''獲取行數列數'''
 5         hanglie = df.shape
 6         hang = hanglie[0]
 7         lie = hanglie[1]
 8         print(hanglie)
 9         print(hang)
10         print(lie)
11         '''
第i行第j列的值''' 12 n = 0 13 m = 1 14 h_list = [] 15 h_lists = [] 16 while n < hang: 17 while m < lie: 18 valueij = df.values[n, 0] 19 h_list.append(valueij) 20 21 valueij = df.values[n, m] 22 h_list.append(valueij)
23 #print(h_list) 24 25 h_lists.append(h_list) 26 print(h_lists) 27 h_list = [] 28 m += 1 29 n += 1 30 m = 1 31 32 '''將第i行第j列的值追加到excel中''' 33 tofilepath = "../HSGtotest81_zh2_dange.xlsx
" 34 writer = pd.ExcelWriter(tofilepath) 35 36 result2 = h_lists 37 #result2 = [['a', '2'], ['b', '2'], ['c', '4']] 38 df = pd.DataFrame(result2) 39 df.to_excel(writer, sheet_name="allhsgmlb", index=False) 40 writer.save() 41 writer.close()