使用循环从数据框粘贴到Excel列

时间:2017-10-12 20:22:51

标签: python python-3.x pandas pandas.excelwriter

我想按特定顺序从Excel文件中复制列,然后以一定间隔将我连接到数据框df1的列粘贴到另一个excel文件中。

换句话说,在数据框中,将第一列从A1粘贴到excel文件中的A1,将第二列粘贴到A3到第三列A5 ...(假设我们已经粘贴了50个这样的列)。到目前为止我已经使用过这些代码了。但是我在这一点上受阻了。在此先感谢您的帮助。

import os
import pandas as pd
from os.path import expanduser

os.chdir('C:\Table')  

files = os.listdir('C:\Table')

print('List of files at *.xls ve *.xlsx format:\n', files)

all_files = [f for f in files if (f[-3:] == 'xls' or f[-4:] == 'xlsx')]

df1 = pd.DataFrame() # Creating empty dataframe

for f in all_files:
    # Take values on C column
    names= pd.read_excel(f, skiprows=1, parse_cols="C:C",sheetname='Sheet1', header=None)
    df1 = pd.concat([df1, names[:1]], axis=1) 

print(df1)

home = expanduser("~\Desktop") #For saving desktop

Saving = input("Please Insert Name Of File:")

writer = pd.ExcelWriter(os.path.join(home,Saving+'.xlsx'), engine='xlsxwriter')
df1.to_excel(writer,startcol=1,startrow=5, sheet_name='Sheet1', header=None, index=False)
workbook = writer.book
worksheet = writer.sheets['Sheet1']
writer.save()

0 个答案:

没有答案