gpt4 book ai didi

python - 在缺少第一个和最后一个值的情况下用 Pandas 插入两个方向?

转载 作者:行者123 更新时间:2023-12-04 14:56:57 24 4
gpt4 key购买 nike

我在尝试通过 pandas 插入电力需求数据时遇到了一个小问题。我有我国 2020、2025、2030、2035 和 2040 年的年度电力消耗预测。我需要做的是将这些预测向后延长至 2017 年,向前延长至 2050 年。在下面的代码中,您可以看到我最初的预测数据框。

Year    Demand1 (TWh)   Demand2 (TWh)   Demand3 (TWh) 
2017 NaN NaN NaN
2018 NaN NaN NaN
2019 NaN NaN NaN
2020 305.0 305.0 305.0
2021 NaN NaN NaN
2022 NaN NaN NaN
2023 NaN NaN NaN
2024 NaN NaN NaN
2025 366.0 370.0 373.0
2026 NaN NaN NaN
......
2030. 427.0 440.0 450.000000
......
2035 485.0 507.0 527.000000
......
2040 545.0 591.0 636.000000

所以,基本上,我正在尝试填充这些 NaN 值。但是,当我尝试使用下面的代码应用插值时,我找不到预期的结果。

demand['Demand1 (TWh)'] = demand['Demand1 (TWh)'].interpolate(method="linear",limit_direction='both')
demand['Demand2 (TWh)'] = demand['Demand2 (TWh)'].interpolate(method="linear",limit_direction='both')
demand['Demand3 (TWh)'] = demand['Demand3 (TWh)'].interpolate(method="linear",limit_direction='both')
demand



Year Demand1 (TWh) Demand2 (TWh) Demand3 (TWh)
2017 305.0 305.0 305.000000
2018 305.0 305.0 305.000000
2019 305.0 305.0 305.000000
2020 305.0 305.0 305.000000
2021 317.2 318.0 317.683429
2022 329.4 331.0 330.825143
2023 341.6 344.0 344.425143
2024 353.8 357.0 358.483429
2025 366.0 370.0 373.000000
2026 378.2 384.0 387.974857
2027 390.4 398.0 403.408000
2028 402.6 412.0 419.201143
2029 414.8 426.0 434.764571
2030 427.0 440.0 450.000000
2031 438.6 453.4 464.907429
2032 450.2 466.8 479.486857
2033 461.8 480.2 493.968000
2034 473.4 493.6 509.729143
2035 485.0 507.0 527.000000
2036 497.0 523.8 545.780571
2037 509.0 540.6 566.070857
2038 521.0 557.4 587.870857
2039 533.0 574.2 611.180571
2040 545.0 591.0 636.000000
2041 545.0 591.0 636.000000
2042 545.0 591.0 636.000000
2043 545.0 591.0 636.000000
2044 545.0 591.0 636.000000
2045 545.0 591.0 636.000000
2046 545.0 591.0 636.000000
2047 545.0 591.0 636.000000
2048 545.0 591.0 636.000000
2049 545.0 591.0 636.000000
2050 545.0 591.0 636.000000

但如您所见,2017-2018-2019 值与 2020 年相同,从 2041 到 2050 年所有值都与 2040 年相同。我不明白为什么以及如何解决它。我将不胜感激任何帮助。谢谢!

最佳答案

我认为这里的问题是 2020 年只有一组值可以从 2017 年到 2020 年进行插值,而 2040 年只有一组值可以从 2040 年到 2050 年进行插值。如果您不希望这些值都相同,您要么必须接受模型的限制,要么在 2017 年或之前以及 2050 年或之后添加数据。

关于python - 在缺少第一个和最后一个值的情况下用 Pandas 插入两个方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67773127/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com