从多个excel文件导入数据

时间:2017-09-15 15:13:42

标签: python excel pandas

import pandas as pd
data = pd.read_excel("file1.xlsx", parse_cols="A,C,E,G, I, K, M, O, Q, S, U, W, Y, AA, AC, AE, AG, AI, AK, AM, AO, AQ, AS, AU, AW, AY, BA, BC, BE, BG, BI, BK, BM, BO, BQ, BS, BU, BW, BY, CA, CC, CE, CG, CI, CK, CM, CO, CQ, CS, CU, CW, CY, DA, DC, DE, DG, DI, DK, DM, DO, DQ, DS, DU, DW, DY, EA, EC, DE, EG, EI, EK, EM, EO, EQ, ES, EU, EW, EY")
data = data.to_string()

如何更改此代码以读取多个Excel文件? file1.xlsx file2.xlsx file5.xlsx

1 个答案:

答案 0 :(得分:2)

定义一个函数来读取.xlsx文件,然后在列表推导中调用它。

import pandas as pd

def parse(file):
    data = pd.read_excel(file, parse_cols="...")
    return data.to_string()

data = [parse(f) for f in ['file1.xlsx', 'file2.xlsx', 'file5.xlsx']]