CASE 1 - When the index address has been redefined with non-numeric values.
#Slicing subseries from a series which has been reindexed with non-numeric values import pandas scalarvalue=[100, 200, 300, 400, 100] print("The Scalar Value List is = ", scalarvalue) S1=pandas.Series(scalarvalue, index=pandas.Index(['A','B','C','D','E'])) S1['A': ]A 100 B 200 C 300 D 400 E 100 dtype: int64S1['A':'D'] --A 100 B 200 C 300 D 400 dtype: int64S1['A':'H'] --A 100 B 200 C 300 D 400 E 100 dtype: int64S1['J':] -- Series([], dtype: int64)S1['5':] --A 100B 200 C 300 D 400 E 100 dtype: int64S1[ :'A'] -- A 100dtype: int64S1[:'C'] --A 100 B 200 C 300 dtype: int64S1[:'H'] --A 100 B 200 C 300 D 400 E 100 dtype: int64S1[:'a'] --A 100 B 200 C 300 D 400 E 100 dtype: int64S1[:'p'] --A 100 B 200 C 300 D 400 E 100 dtype: int64S1[:'6'] --Series([], dtype: int64)S1[:] --A 100 B 200 C 300 D 400 E 100 dtype: int64SLICING THROUGH POSITIONAL POSITIVE INDEX# Slicing subseries from a series which has been reindexed with repetitive non-numeric values through positional positive indeximport pandas scalarvalue=[100, 200, 300, 400, 100] print("The Scalar Value List is = ", scalarvalue) S1=pandas.Series(scalarvalue, index=pandas.Index(['A','B','C','D','E'])) S1[0:]A 100 B 200 C 300 D 400 E 100 dtype: int64S1[0:3] --A 100 B 200 C 300 dtype: int64S1[0:8] --A 100 B 200 C 300 D 400 E 100 dtype: int64S1[6:10] -- Series([], dtype: int64)S1[0:-2] --A 100 B 200 C 300 dtype: int64S1[0:-4] --A 100 dtype: int64S1[0:-6] --Series([], dtype: int64)SLICING THROUGH POSITIONAL NEGATIVE INDEX# Slicing subseries from a series which has been reindexed with non-numeric values through positional negative indeximport pandas scalarvalue=[100, 200, 300, 400, 100] print("The Scalar Value List is = ", scalarvalue) S1=pandas.Series(scalarvalue, index=pandas.Index(['A','B','C','D','E'])) S1[-1:]E 100 dtype: int64S1[-2:] --D 400E 100 dtype: int64S1[-5:] --A 100 B 200 C 300 D 400 E 100 dtype: int64S1[:-2] --A 100 B 200 C 300 dtype: int64S1[:-6] --Series([], dtype: int64)S1[-1:-6] --Series([], dtype: int64)S1[-6:-3] --A 100 B 200 dtype: int64S1[-6:-4] --A 100 dtype: int64S1[-6:-6] --Series([], dtype: int64)# Slicing subseries from a series which has been reindexed with repetitive non-numeric values through positional negative indeximport pandas scalarvalue=[100, 200, 300, 400, 100] print("The Scalar Value List is = ", scalarvalue) S1=pandas.Series(scalarvalue, index=pandas.Index(['E','D','A','D','E'])) print(S1)E 100 D 200 A 300 D 400 E 100 dtype: int64S1['A':] --A 300D 400 E 100 dtype: int64S1['A':'E'] --KeyError: "Cannot get right slice bound for non-unique label: 'E'"S1['E':'A']KeyError: "Cannot get left slice bound for non-unique label: 'E'"S1['E':'D'] --KeyError: "Cannot get left slice bound for non-unique label: 'E'"S1['D':'E'] --KeyError: "Cannot get left slice bound for non-unique label: 'D'"CASE 2 - When the index address has been redefined with a new list of numeric values.
#Slicing subseries through new numeric index address from a series which has been reindexed with new numeric values
import pandas
scalarvalue=[100, 200, 300, 400, 100]
print("The Scalar Value List is = ", scalarvalue)
S1=pandas.Series(scalarvalue, index=pandas.Index([10,20,30,40,50]))
S1
10 100 20 200 30 300 40 400 50 100 dtype: int64
S1[20: ] - Series([], dtype: int64)
S1[ :40] --
10 100 20 200 30 300 40 400 50 100 dtype: int64
** This shows an output because 40 is considered as the last specified positional positive index address and not as the labelled index address.
S1[40:10] -- Series([], dtype: int64)S1[0:4] --
10 100
20 200 30 300 40 400 dtype: int64
S1[2:4] --
30 300 40 400 dtype: int64
Extracting an Element from a Series created from a dictionary.
Default Case (Extraction through the labelled index address which are the keys)
Consider the Series - MySeries1
import pandas as pd
nd={ 'd1' : { 'Stud1' : 100, ' Stud2 ' : 95},
'd2' : { 'Stud3' : 95, ' Stud4 ' : 98}
}
MySeries1= pd.Series(nd)
MySeries1
d1 {'Stud1': 100, ' Stud2 ': 95}
d2 {'Stud3': 95, ' Stud4 ': 98}
dtype: object
MySeries1['d1'] --{'Stud1': 100, ' Stud2 ': 95}MySeries1['d2'] --{'Stud3': 95, ' Stud4 ': 98}Case 1 (Extraction through the positional positive index address)MySeries1[0] -- {'Stud1': 100, ' Stud2 ': 95}MySeries1[2] -- IndexError: index 2 is out of bounds for axis 0 with size 2Case 2 (Extraction through the positional negative index address)MySeries1[-1] -- {'Stud3': 95, ' Stud4 ': 98}MySeries1[-2] -- {'Stud1': 100, ' Stud2 ': 95}MySeries1[-4] -- IndexError: index -4 is out of bounds for axis 0 with size 2Slicing a subseries from a Series created from a dictionary
Default Case (through default index - keys)MySeries1['d1':] --d1 {'Stud1': 100, ' Stud2 ': 95} d2 {'Stud3': 95, ' Stud4 ': 98} dtype: objectMySeries1['d3': ] --Series([], dtype: object)MySeries1[ :'d1'] --d1 {'Stud1': 100, ' Stud2 ': 95} dtype: objectMySeries1[ :'d2'] --d1 {'Stud1': 100, ' Stud2 ': 95} d2 {'Stud3': 95, ' Stud4 ': 98}Case 1 (Slicing through the positional positive index address)MySeries1[0:2] --d1 {'Stud1': 100, ' Stud2 ': 95} d2 {'Stud3': 95, ' Stud4 ': 98} dtype: objectMySeries1[1:2] --d2 {'Stud3': 95, ' Stud4 ': 98} dtype: objectMySeries1[2:4] --Series([], dtype: object)Case 2 (Slicing through the positional negative index address)MySeries1[-1:] --d2 {'Stud3': 95, ' Stud4 ': 98} dtype: objectMySeries1[-2:] --d1 {'Stud1': 100, ' Stud2 ': 95} d2 {'Stud3': 95, ' Stud4 ': 98} dtype: objectMySeries1[-1:-2] --Series([], dtype: object)MySeries1[-2:-1] --d1 {'Stud1': 100, ' Stud2 ': 95} dtype: objectMySeries1[-3:-2] --Series([], dtype: object)********************************************************************************
