You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将指定时间范围的Close列数据按升序打印?

解决方法:将时间序列按升序打印

要让输出的时间序列按升序(从早到晚)排列,你可以通过以下几种方式实现:

  • 直接对索引排序:因为你的数据是按日期索引降序输出的,调用sort_index()方法即可将索引(日期)按升序排列:

    df['2020-01': '2020-02']['Close'].sort_index().head(50)
    

    如果需要明确指定升序规则,可以加上ascending=True参数(默认即为True):

    df['2020-01': '2020-02']['Close'].sort_index(ascending=True).head(50)
    
  • 使用规范的索引方式避免警告:如果遇到SettingWithCopyWarning,可以改用loc进行切片,再排序:

    df.loc['2020-01':'2020-02', 'Close'].sort_index().head(50)
    

执行以上代码后,输出的日期就会从最早的时间开始,按升序显示前50条Close数据。

内容的提问来源于stack exchange,提问作者Indranil_Palit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 19:06:20