Given a series like pd.Series([1,2,3,4])
To get the first value I can do:
first = series[0]
But to get the last value like last = series[-1]
produces an error.
I need to do last = series.iloc[-1]
I don't understand why. (Why do I don't need first = series.iloc[0]
or can do last = series[-1]
?
Thank you.
CodePudding user response:
To answer your question, let's do a little experiement.
s = pd.Series([1,2,3,4],index=[9,8,7,6])
s.index
Output:
Int64Index([9, 8, 7, 6], dtype='int64')
If you have this series, even S[0] will give an error. But you can do S[9], S[8], etc..
s[9]
1
s[8]
2
s[0]
KeyError: 0
s[-1]
KeyError: -1
When you are using s[0], you are using the index value which does not exist. Same for s[-1] in this case.
When you want to use -1 to refer to the last item, you can still do it but like this:
s = pd.Series([1,2,3,4])
s[-1:]
Output:
3 4
dtype: int64
You are telling the series, you want the last item till the end, not specifing the actual index value. In this case, 3 is the index, 4 is the value.
And if you want to refer to the 2nd last item, like this:
s = pd.Series([1,2,3,4])
s[-2:-1]
Output:
2 3
dtype: int64