python 在 'variable name list' 中通过 str 调用变量(连接 100 个数据帧)

python call variables by str in the 'variable name list' (concat 100 dataframes)

#variables for df
a = pd.read_excel("/Downloads/1.xls")
b = pd.read_excel("/Downloads/2.xls")
c = pd.read_excel("/Downloads/3.xls")
d = pd.read_excel("/Downloads/4.xls")
e = pd.read_excel("/Downloads/5.xls")

#I want to concat them
pd.concat((a,b,c,d,e))

但问题是变量名太长,大约有100个dataframe变量。 因此我想:

variables_list = ['a' ,'b', 'c', 'd', 'e']
pd.concat(variables_list)

当然不行。我只想解释我想做什么。任何人都知道如何在 python?

中一次连接大约 100 个数据帧

您可以使用 python 的 list

import pandas as pd

a = []
for i in range(download_num):
    a.append(pd.read_excel("/Downloads/{}.xls".format(i)))

concatenated_df = pd.concat(a)

如果您已经文件名列表

import pandas as pd

a = []
for fn in filenames:
    a.append(pd.read_excel("/Downloads/" + fn))

concatenated_df = pd.concat(a)