我需要匯入 3 個 .xlsx 檔案并將它們合并到 1 個 DataFrame 中。我想避免使用 for 回圈重復代碼。
原始代碼:
filepath_1 = input('Enter Revenue Month M1 File Path: ')
revenue_month_1 = pd.read_excel(filepath_1)
revenue_month_1 = revenue_month_1.apply(pd.to_numeric, errors='ignore')
revenue_month_1['Month'] = pd.to_datetime(revenue_month_1['Month'], format='%Y%m', errors='coerce').dropna()
filepath_2 = input('Enter Revenue Month M2 File Path: ')
revenue_month_2 = pd.read_excel(filepath_2)
revenue_month_2 = revenue_month_2.apply(pd.to_numeric, errors='ignore')
revenue_month_2['Month'] = pd.to_datetime(revenue_month_2['Month'], format='%Y%m', errors='coerce').dropna()
filepath_3 = input('Enter Revenue Month M3 File Path: ')
revenue_month_3 = pd.read_excel(filepath_3)
revenue_month_3 = revenue_month_3.apply(pd.to_numeric, errors='ignore')
revenue_month_3['Month'] = pd.to_datetime(revenue_month_3['Month'], format='%Y%m', errors='coerce').dropna()
帶有 for 回圈的代碼:
revenue_reports = [
input('Enter Revenue Month M1 File Path: '),
input('Enter Revenue Month M2 File Path: '),
input('Enter Revenue Month M3 File Path: '),
]
revenue = []
for revenue_report in revenue_reports:
revenue = pd.read_excel(revenue_report)
revenue = revenue.apply(pd.to_numeric, errors='ignore')
revenue['Month'] = pd.to_datetime(revenue['Month'], format='%Y%m', errors='coerce').dropna()
revenue = revenue.append(revenue)
基于此 for 回圈,我僅從匯入的 3 個月資料中獲取上個月資料 (M3)。你能幫忙嗎?
更新:問題已解決。感謝下面評論中的想法。我稍微修改了一下。它看起來像這樣:
revenue_reports = [
input('Enter Revenue Month M1 File Path: '),
input('Enter Revenue Month M2 File Path: '),
input('Enter Revenue Month M3 File Path: '),
]
revenue = []
x = 1
for revenue_report in revenue_reports:
revenue_monthly = pd.read_excel(revenue_report)
revenue_monthly = revenue_monthly.apply(pd.to_numeric, errors='ignore')
revenue_monthly["M" str(x)] = pd.to_datetime(revenue_monthly['Month'], format='%Y%m', errors='coerce').dropna()
x = 1
revenue.append(revenue_monthly)
revenue = pd.concat(revenue)
uj5u.com熱心網友回復:
你的代碼很好,但問題是你用前一個替換了新值,這就是為什么你只得到 M3 的最后一個值
試試這個
revenue_reports = [
input('Enter Revenue Month M1 File Path: '),
input('Enter Revenue Month M2 File Path: '),
input('Enter Revenue Month M3 File Path: '),
]
revenue = {}
x = 1
for revenue_report in revenue_reports:
revenue_val = pd.read_excel(revenue_report)
revenue_val = revenue_val.apply(pd.to_numeric, errors='ignore')
revenue["M" x] = pd.to_datetime(revenue_val['Month'], format='%Y%m', errors='coerce').dropna()
x = 1
uj5u.com熱心網友回復:
只能使用新變數來存盤所有收入資料。
revenue_reports = [
input('Enter Revenue Month M1 File Path: '),
input('Enter Revenue Month M2 File Path: '),
input('Enter Revenue Month M3 File Path: '),
]
All_revenue =[]
revenue = []
for revenue_report in revenue_reports:
revenue = pd.read_excel(revenue_report)
revenue = revenue.apply(pd.to_numeric, errors='ignore')
revenue['Month'] = pd.to_datetime(revenue['Month'], format='%Y%m', errors='coerce').dropna()
All_revenue = All_revenue.append(revenue)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/328273.html
上一篇:恢復公式的顯示結果
下一篇:VBA代碼匯總每個客戶商品的價格
