宏观数据整理

import akshare as ak
import pandas as pd

# 将各个数据集放入列表中,方便循环处理
data_frames = [
    ak.macro_usa_core_cpi_monthly(),
    ak.macro_usa_core_pce_price(),
    ak.macro_usa_gdp_monthly(),
    ak.macro_usa_non_farm(),
    ak.macro_usa_unemployment_rate(),
    ak.macro_china_cpi_yearly(),
    ak.macro_china_ppi_yearly(),
    ak.macro_china_pmi_yearly()
]

# 用于存储处理后的最新行数据
latest_rows = []

# 循环遍历每个数据集
for df in data_frames:
    latest_row = df.iloc[-1]
    if pd.isna(latest_row.iloc[2]):
        latest_row = df.iloc[-2]
    latest_rows.append(latest_row)

# 构造新的数据,组合成新表
new_data = {
    "商品": ["美国核心CPI", "美国核心PCE价格", "美国月度GDP","美国非农","美国失业率","中国CPI同比","中国PPI同比","中国PMI"],
    "日期": [r.iloc[1] for r in latest_rows],
    "今值": [r.iloc[2] for r in latest_rows]
}

new_table = pd.DataFrame(new_data)
print(new_table)

  

posted @ 2024-12-23 14:41  方木--数据分析与挖掘  阅读(5)  评论(0编辑  收藏  举报