【问题标题】:How to choose last several rows of python series? [duplicate]如何选择python系列的最后几行? [复制]
【发布时间】:2017-02-15 00:59:36
【问题描述】:

我有一个 python 系列,我想选择它的最后 5 行。如果我使用 ts[-5:-1],它不会返回最后一行。我该怎么做?谢谢!

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    使用tail:

    s.tail(5)
    

    iloc:

    s.iloc[-5:]
    

    示例:

    s = pd.Series(range(10))    
    print (s)
    0    0
    1    1
    2    2
    3    3
    4    4
    5    5
    6    6
    7    7
    8    8
    9    9
    dtype: int32
    
    print (s.tail(5))
    5    5
    6    6
    7    7
    8    8
    9    9
    dtype: int32
    
    print (s.iloc[-5:])
    5    5
    6    6
    7    7
    8    8
    9    9
    dtype: int32
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-10
      • 2016-01-19
      • 2021-07-08
      • 1970-01-01
      • 2013-12-27
      • 2015-01-23
      • 2013-03-19
      • 1970-01-01
      相关资源
      最近更新 更多