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()

 

posted @ 2022-03-22 14:46  种太阳  阅读(667)  评论(0编辑  收藏  举报