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)
添加回答
舉報