Domanda

I have multiple excel files with extension .xls and in need to combine or append them vertically. Now I have header in all of them but want to remove it from all of them except the First One. The name of files are SWEdtd01MAY14NUM1.xls to SWEdtd01MAY14NUMn.xls where n varies based on the file size.

Will it be possible to do it through KSH / Python / SAS.

È stato utile?

Soluzione

Below is an example of how to do it with Python and library Pandas

Several things may have to be tuned up, to match the needs, but the basic idea is there

# Script to concatenate a bunch of Excel files with
# Python and Pandas
#
# Remember that indexing starts with 0 in Python,
# whereas indexing starts with 1 in Excel

import pandas as pd
# Number of files to process
n = 10
# Excel sheetname
sheetname = 'sheet1'
# Number of row to skip in each file
skiprows=3
# Header line that will be kept for column name (index 5 in Excel)
header=4
# Column containing the index for each row. Leave it to None if no index
index_col=0
# First file to process
f = 'SWEdtd01MAY14NUM1.xls'
DF = pd.read_excel(f, sheetname, skiprows = skiprows,header = header, index_col = index_col)
# Concatenate the content of other file to this dataframe
for i in range(2,n+1)
    f = 'SWEdtd01MAY14NUM'+str(i)+'.xls'
    df = pd.read_excel(f, sheetname, skiprows = skiprows, header = header, index_col = index_col)
    DF.append(df, ignore_index=True)
# Write the concatenated content to excel
DF.to_excel('SWEdtd01MAY14NUM.xls',sheet_name = sheetname)

Altri suggerimenti

You can do it entirely in python using pandas

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top