I want to copy the columns from an Excel file in a certain order, and then paste the columns I concatenated to the data frame df1
into another excel file at a certain interval.
In other words, in the dataframe, paste the first column from A1 to A1 in the excel file, the second column to A3 to the third column A5 ... (assuming we have pasted 50 such columns). I've used these codes so far. But I'm blocked at this point. Thanks in advance for your help.
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()