1

I have a python series and I want to choose the last 5 rows of it. If I use ts[-5:-1],it doesn't return the last rows. How can I do it? Thanks!

Jiaqi
  • 51
  • 1
  • 1
  • 4

1 Answers1

3

Use tail:

s.tail(5)

or iloc:

s.iloc[-5:]

Sample:

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252