我有多个扩展名为 .xls 的excel文件,需要将它们垂直合并或追加。现在我在所有这些中都有标题,但是除了第一个之外,我想将它从所有这些中删除。文件名为 SWEdtd01MAY14NUM1.xls至SWEdtd01MAY14NUMn.xls ,其中 n 因文件大小而异。
是否可以通过KSH / Python / SAS来实现。
答案 0 :(得分:1)
下面是如何使用Python和库Pandas
的示例为了满足需求,可能需要调整几个方面,但基本的想法是
# 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)
答案 1 :(得分:0)
你可以使用pandas
在python中完全完成