【问题标题】:Pandas DataReader Error = Length mismatch: Expected axis has 8 elements, new values have 7 elementsPandas DataReader 错误 = 长度不匹配:预期轴有 8 个元素,新值有 7 个元素
【发布时间】:2025-12-22 01:30:06
【问题描述】:

当我想从 St.Louis Fed 导入数据时,Python 给了我这样的错误:

长度不匹配:预期轴有 8 个元素,新值有 7 个元素。

我可以通过删除dataFrame的最后一列来解决这个错误,但我需要它。我该怎么办?

import matplotlib.pyplot as plt
import pandas_datareader.data as pdr
import pandas as pd
import datetime

start = datetime.datetime(2000, 1, 1)
end = datetime.datetime(2021, 5, 1)
df = pdr.DataReader(
    [
        "WFRBSB50215",
        "WFRBST01134",
        "WFRBST01122",
        "WFRBSN09139",
        "WFRBSB50189",
        "WFRBST01110",
        "WFRBSB50191",
        "WFRBSN09148",
    ],
    "fred",
    start,
    end,
)
df.columns = [
    "Share of Total Net Worth Held by the Bottom 50% (1st to 50th Wealth Percentiles)",
    "Share of Total Net Worth Held by the Top 1% (99th to 100th Wealth Percentiles)",
    "Share of Corporate Equities and Mutual Fund Shares Helb By Top1%(99th to 100th Wealth Percentiles)",
    "Share of Financial Assets Held by the 90th to 99th Wealth Percentiles",
    "Share of Total Assets Held by the Bottom 50% (1st to 50th Wealth Percentiles)",
    "Share of Real Estate Held by the Top 1% (99th to 100th Wealth Percentiles)",
    "Share of Real Estate Held by the Bottom %50(1st to 50th Wealth Percentiles)"
    "Share of Mortgages Held by the 90th to 99th Wealth Percentiles",
]
ax = df.plot(subplots=True, figsize=(12, 12), linewidth=3.5, colormap="summer")
plt.xlabel("Date")
ax[
    0,
].set_title("Share of Total Net Worth Held by the Bottom 50%")
ax[
    0,
].set_ylabel("Percent of Aggregate")
ax[
    1,
].set_title("Share of Total Net Worth Held by the Top 1%")
ax[
    1,
].set_ylabel("Percent of Aggregate")
ax[
    2,
].set_title("Share of Corporate Equities and Mutual Fund Shares Helb By Top 1%")
ax[
    2,
].set_ylabel("Percent of Aggregate")
ax[
    3,
].set_title("Share of Financial Assets Held by the 90th to 99th Wealth Percentiles")
ax[
    3,
].set_ylabel("Percent of Aggregate")
ax[
    4,
].set_title("Share of Total Assets Held by the Bottom 50% ")
ax[
    4,
].set_ylabel("Percent of Aggregate")
ax[
    5,
].set_title("Share of Real Estate Held by the Top 1%")
ax[
    5,
].set_ylabel("Percent of Aggregate")
ax[
    6,
].set_title("Share of Real Estate Held by the Bottom %50")
ax[
    6,
].set_ylabel("Percent of Aggregate")
ax[
    7,
].set_title("Share of Mortgages Held by the 90th to 99th Wealth Percentiles")
ax[
    7,
].set_ylabel("Percent of Aggregate")
plt.tight_layout()
plt.show()

【问题讨论】:

    标签: python pandas pandas-datareader


    【解决方案1】:

    您在新的df.columns 中倒数第二个项目后缺少逗号。

    没有它,不同行上的字符串会聚集在一起,总共有 7 个元素,而不是预期的 8 个。

    【讨论】:

    • 你的眼睛比老鹰还好,非常感谢:))
    最近更新 更多