使用 Pandas 操作資料
你可以把來自不同來源的資料合併成單一的 DataFrame。
想像你要計算 GDP,以掌握美國經濟的健康狀況。你已經從不同來源、不同格式蒐集到所需的資料。
你可以使用提供的 DataFrame,分別代表個人消費支出、政府支出、民間固定資本形成,以及淨出口,來計算國內生產毛額。已提供的 DataFrame 有 ge、gpdi、ne 和 pce。
本練習屬於課程
Intermediate Python for Finance
練習說明
- 依序將提供的來源 DataFrame
ge、gpdi、ne與pce合併為一個新的 DataFrame。 - 對每一列加總其數值,得到每年的 GDP。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Combine the source DataFrames into one
gdp = pd.____([ge, gpdi, ____, ____], axis=1)
# Add the columns and create a new column with the result
gdp['GDP'] = gdp.____(np.sum, axis=1)