1 回答

TA贡献1735条经验 获得超5个赞
您可以使用或列表理解创建系列列表append
,然后keys
在中使用参数concat
:
import glob, os
my_excel_files = glob.glob(r"C:\Users\......\Documents\*.xlsx")
names = [os.path.basename(f).split('.')[0] for f in my_excel_files]
output = []
for file in my_excel_files:
df = pd.read_excel(file, header = 1)
new_df = df['Comments']
output.append(new_df)
final = pd.concat(output, axis=1, keys=names)
或者:
import glob, os
my_excel_files = glob.glob(r"C:\Users\......\Documents\*.xlsx")
names = [os.path.basename(f).split('.')[0] for f in my_excel_files]
output = [pd.read_excel(file, header = 1)['Comments'] for file in my_excel_files]
final = pd.concat(output, axis=1, keys=names)
添加回答
举报