0

我想以特定顺序从 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()
4

0 回答 0