In [12]: #[] subscript method is used to access series eleents with the help of default index
import pandas as pd
s=pd.Series(range(100,500,50))
s
Out[12]: 0 100
1 150
2 200
3 250
4 300
5 350
6 400
7 450
dtype: int64
In [13]: s[1]
Out[13]: 150
In [14]: s[0:5]
Out[14]: 0 100
1 150
2 200
3 250
4 300
dtype: int64
In [15]: s[0:6:2]
Out[15]: 0 100
2 200
4 300
dtype: int64
In [21]: s[-1]
#plz note that if we are passing single negative index it raises an error
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
File ~\anaconda3\Lib\site-packages\pandas\core\indexes\range.py:391, in RangeIndex.get_loc(self, key, method, tolerance)
390 try:
--> 391 return self._range.index(new_key)
392 except ValueError as err:
ValueError: -1 is not in range
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
Cell In[21], line 1
----> 1 s[-1]
File ~\anaconda3\Lib\site-packages\pandas\core\series.py:981, in Series.__getitem__(self, key)
978 return self._values[key]
980 elif key_is_scalar:
--> 981 return self._get_value(key)
983 if is_hashable(key):
984 # Otherwise index.get_value will raise InvalidIndexError
985 try:
986 # For labels that don't resolve as scalars like tuples and frozensets
File ~\anaconda3\Lib\site-packages\pandas\core\series.py:1089, in Series._get_value(self, label, takeable)
1086 return self._values[label]
1088 # Similar to Index.get_value, but we do not fall back to positional
-> 1089 loc = self.index.get_loc(label)
1090 return self.index._get_values_for_loc(self, loc, label)
File ~\anaconda3\Lib\site-packages\pandas\core\indexes\range.py:393, in RangeIndex.get_loc(self, key, method, tolerance)
391 return self._range.index(new_key)
392 except ValueError as err:
--> 393 raise KeyError(key) from err
394 self._check_indexing_error(key)
395 raise KeyError(key)
KeyError: -1
In [22]: s[-4:]
Out[22]: 4 300
5 350
6 400
7 450
dtype: int64
In [23]: s[-6:-1]
Out[23]: 2 200
3 250
4 300
5 350
6 400
dtype: int64
In [24]: s[-6:-1:-2]
Out[24]: Series([], dtype: int64)
In [25]: s[-6:-1:2]
Out[25]: 2 200
4 300
6 400
dtype: int64
In [26]: s[4:8:2]
Out[26]: 4 300
6 400
dtype: int64
In [27]: s[4:9:-2]
Out[27]: Series([], dtype: int64)
In [28]: s[:]
#shows complete series
Out[28]: 0 100
1 150
2 200
3 250
4 300
5 350
6 400
7 450
dtype: int64
In [29]: s[::]
#shows complete series
Out[29]: 0 100
1 150
2 200
3 250
4 300
5 350
6 400
7 450
dtype: int64
In [30]: s[::-1]
#reverse the series
Out[30]: 7 450
6 400
5 350
4 300
3 250
2 200
1 150
0 100
dtype: int64
In [31]: s[::-2]
#every 2nd value to be taken as step is 2
Out[31]: 7 450
5 350
3 250
1 150
dtype: int64
In [33]: s[::-3]
#every 3rd value to be taken as step is 3
Out[33]: 7 450
4 300
1 150
dtype: int64
In [34]: s[:5]
#As start value is not given it will start fropm 0
Out[34]: 0 100
1 150
2 200
3 250
4 300
dtype: int64
In [35]: s[3:]
#AS stop value is not given it will stop at the end
Out[35]: 3 250
4 300
5 350
6 400
7 450
dtype: int64
In [3]: #[] subscript method is used to access series eleents with the help of user defined index
import pandas as pd
st=pd.Series([10,20,30],index=['a','x','c'])
st['a']
Out[3]: 10
In [4]: st['a':'c']
#Plz note that in case of user defined index stop value is included
Out[4]: a 10
x 20
c 30
dtype: int64
In [5]: st[0:2]
#Plz note that even if user defined index is there we still can use default index
#But stop value is not included
Out[5]: a 10
x 20
dtype: int64
In [9]: #use of step value in series object with user defined index.
st['a':'c':2]
Out[9]: a 10
c 30
dtype: int64
In [ ]: