2015-05-06 83 views
0

大家好...使用Panda结合Excel电子表格的问题。Python结合Excel电子表格

问题是,列的顺序在组合时会丢失。如果有更多的文件需要组合,格式会更糟。

如果给出错误信息,如果文件数量很大。

ValueError: column index (256) not an int in range(256) 

我现在用的就是如下:

import pandas as pd 

df = pd.DataFrame() 

for f in ['c:\\1635.xls', 'c:\\1644.xls']: 
    data = pd.read_excel(f, 'Sheet1') 
    data.index = [os.path.basename(f)] * len(data) 
    df = df.append(data) 

df.to_excel('c:\\CB.xls') 

原始文件和联合的样子: enter image description here

什么是结合这种类似Excel文件的大量的最好方法是什么?

谢谢。

回答

1

我通常使用xlrdxlwt

#!/usr/bin/env python 
# encoding: utf-8 

import xlwt 
import xlrd 
import os 


current_file = xlwt.Workbook() 
write_table = current_file.add_sheet('sheet1', cell_overwrite_ok=True) 

key_list = [u'City', u'Country', u'Received Date', u'Shipping Date', u'Weight', u'1635'] 
for title_index, text in enumerate(key_list): 
    write_table.write(0, title_index, text) 


file_list = ['1635.xlsx', '1644.xlsx'] 

i = 1 
for name in file_list: 
    data = xlrd.open_workbook(name) 

    table = data.sheets()[0] 
    nrows = table.nrows 
    for row in range(nrows): 
     if row == 0: 
      continue 
     for index, context in enumerate(table.row_values(row)): 
      write_table.write(i, index, context) 
     i += 1 


current_file.save(os.getcwd() + '/result.xls') 
+0

非常直接的逻辑答案感谢。 –

1

相反data.index = [os.path.basename(f)] * len(data)的,你应该使用df.reset_index()

例如:

1.xlsx:

a b 
1 1 
2 2 
3 3 

2.xlsx:

a b 
4 4 
5 5 
6 6 

代码:

df = pd.DataFrame() 

for f in [r"C:\Users\Adi\Desktop\1.xlsx", r"C:\Users\Adi\Desktop\2.xlsx"]: 
    data = pd.read_excel(f, 'Sheet1') 
    df = df.append(data) 

df.reset_index(inplace=True, drop=True) 
df.to_excel('c:\\CB.xls') 

cb.xls:

 a b 
    0 1 1 
    1 2 2 
    2 3 3 
    3 4 4 
    4 5 5 
    5 6 6 

如果您不希望数据框的索引位于输出文件中,则可以使用df.to_excel('c:\\CB.xls', index=False)

+0

非常感谢您的帮助。惊人!很难选择你的或更早的答案作为最佳答案。希望你不要介意.. –