首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pyexcel -获取列名并将整行添加到一起

pyexcel -获取列名并将整行添加到一起
EN

Stack Overflow用户
提问于 2016-07-21 12:00:36
回答 1查看 360关注 0票数 0

我有几个速查表:

代码语言:javascript
复制
Sheet 1     sheet2       sheet3

A B C     D E F     D F G

1 2 3      4 5 6     7 9 8

我使用pyexcel将电子表格1和2以及1和3中的行连接在一起,因此1和2的合并行将为:

代码语言:javascript
复制
A B C D E F D F G,
1 2 3 4 5 6  

和1和3:

代码语言:javascript
复制
A B C D E F D F G
1 2 3       7 9 8

如何在pyexcel中做到这一点?

现在我有两个for循环和这个:

代码语言:javascript
复制
 if t_row['name'] is not "":
                    update_sheet[count, 'name'] = t_row['name']

但是工作表2没有F和G列,工作表3没有E和F,我如何列出工作表有哪些列,或者只是取整行并将其与row连接并存储?

EN

回答 1

Stack Overflow用户

发布于 2016-07-21 13:44:35

目前还不清楚:

  1. 您如何阅读工作表
  2. 当两个工作表都具有值时,您希望如何处理联接。我想你想总结一下。

将numpy导入为np导入pyexcel为pe a= np.array(pe.get_array(file_name='Sheet1.xlsx')) b= np.array(pe.get_array(file_name='Sheet2.xlsx')) c= np.array(pe.get_array(file_name='Sheet3.xlsx')) all=a,b,对于范围(3)中的i,c max_cols = max([i.shape1 in i]):if alli.dtype!=np.dtype('int'):allialli=='']=0 alli=alli.astype('int') if (alli.shape1 != max_cols):alli=np.hstack([alli,[*(max_cols-ali.shape1)]*(alli.shape)]) np.sum(np.vstack(all),0)

编辑

使用时不需要for循环(仅用于在不同的工作表中循环)。这将以一种蟒蛇的方式使用numpy!

代码语言:javascript
复制
def join_sheets(a, b):
    both = [a,b]
    max_cols = max([i.number_of_columns() for i in both])
    min_rows = min([i.number_of_rows() for i in both])
    both_arr = [np.array(i.array) for i in both]
    for i in range(2):
        both_arr[i] = np.hstack([both_arr[i], [['']*(max_cols - both_arr[i].shape[1])]*(both_arr[i].shape[0])])
    both_arr[0][0:min_rows,][both_arr[1][0:min_rows,]!=''] = both_arr[1][0:min_rows,][both_arr[1][0:min_rows,]!='']
    if (b.number_of_rows() > min_rows):
        both_arr[0] = np.vstack([both_arr[0], both_arr[1][min_rows:,]])
    a.array = both_arr[0].tolist()

sheets = pe.get_book(file_name='Sheet1.xlsx')
for i in range(1, sheets.number_of_sheets()): join_sheets(sheets[0], sheets[i])
sheets.save_as(sheets.path + '/' + sheets.filename)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38494652

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档