Wednesday, 4 August 2021

Extracting & Slicing Subseries created from a dictionary

 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: int64

S1['A':'D'] -- 
A    100
B    200
C    300
D    400
dtype: int64

S1['A':'H'] --
A    100
B    200
C    300
D    400
E    100
dtype: int64

S1['J':] -- Series([], dtype: int64)

S1['5':] -- 
A    100
B    200
C    300
D    400
E    100
dtype: int64

S1[ :'A'] --  A    100
              dtype: int64

S1[:'C'] --
A    100
B    200
C    300
dtype: int64

S1[:'H'] --
A    100
B    200
C    300
D    400
E    100
dtype: int64

S1[:'a'] --
A    100
B    200
C    300
D    400
E    100
dtype: int64

S1[:'p'] --
A    100
B    200
C    300
D    400
E    100
dtype: int64

S1[:'6'] --
Series([], dtype: int64)

S1[:] --
A    100
B    200
C    300
D    400
E    100
dtype: int64

SLICING THROUGH  POSITIONAL POSITIVE INDEX 

# Slicing subseries from a series which has been reindexed with repetitive non-numeric values through positional positive index

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[0:]

A    100
B    200
C    300
D    400
E    100
dtype: int64

S1[0:3] -- 
A    100
B    200
C    300
dtype: int64


S1[0:8] --
A    100
B    200
C    300
D    400
E    100
dtype: int64

S1[6:10] -- Series([], dtype: int64)

S1[0:-2] --
A    100
B    200
C    300
dtype: int64


S1[0:-4] -- 
A    100
dtype: int64


S1[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 index


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[-1:]


E    100
dtype: int64


S1[-2:] -- 
D    400
E    100
dtype: int64

S1[-5:] --
A    100
B    200
C    300
D    400
E    100
dtype: int64 

S1[:-2]  -- 
A    100
B    200
C    300
dtype: int64

S1[:-6] -- 
Series([], dtype: int64)

S1[-1:-6] --
Series([], dtype: int64)

S1[-6:-3] --
A    100
B    200
dtype: int64

S1[-6:-4] --
A    100
dtype: int64

S1[-6:-6] --
Series([], dtype: int64)


# Slicing subseries from a series which has been reindexed with repetitive non-numeric values through positional negative index

import 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: int64

S1['A':]  -- 
A    300
D    400
E    100
dtype: int64

S1['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 2


Case 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 2



Slicing 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: object


MySeries1['d3': ] --
Series([], dtype: object)


MySeries1[ :'d1'] --
d1    {'Stud1': 100, ' Stud2 ': 95}
dtype: object


MySeries1[ :'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: object


MySeries1[1:2] --
d2    {'Stud3': 95, ' Stud4 ': 98}
dtype: object


MySeries1[2:4] --
Series([], dtype: object) 


Case 2 (Slicing through the positional negative index address)

MySeries1[-1:] --
d2    {'Stud3': 95, ' Stud4 ': 98}
dtype: object

MySeries1[-2:] --
d1    {'Stud1': 100, ' Stud2 ': 95}
d2     {'Stud3': 95, ' Stud4 ': 98}
dtype: object


MySeries1[-1:-2] -- 
Series([], dtype: object)


MySeries1[-2:-1] --
d1    {'Stud1': 100, ' Stud2 ': 95}
dtype: object


MySeries1[-3:-2] --
Series([], dtype: object) 


********************************************************************************
 

Slicing a Subseries from a Series created from Scalar Value List

 Slicing a subseries  (Slicing)


':' - slice operator is used to specify the range of the index address.
m:n - m is the index address from where the extraction begins 
    - n is the index address upto 1 less the extraction has to be done


import pandas

scalarvalue=[100, 200, 300, 400, 100]
print("The Scalar Value List is       = ", scalarvalue)
S1=pandas.Series(scalarvalue)

# Positional indexing
print("The data are  ")
print(S1[0:5])

The Scalar Value List is       =  [100, 200, 300, 400, 100]
The data are  
0    100
1    200
2    300
3    400
4    100
dtype: int64

[100, 200, 300, 400, 100]
print(S1[0:])   --  
The data are  
0    100
1    200
2    300
3    400
4    100
dtype: int64

[100, 200, 300, 400, 100]
print(S1[1:]) --
1    200
2    300
3    400
4    100
dtype: int64

[100, 200, 300, 400, 100]

print(S1[1:3]) --
The data are  
1    200
2    300
dtype: int64


print(S1[:2]) -- 
The data is/are  
0    100
1    200
dtype: int64

print(S1[:6]) --
The data is/are  
0    100
1    200
2    300
3    400
4    100
dtype: int64

** 
Negative Positional Indexing

-m :      = upto the exact negative index address subseries will be located
-m :  n   = will extract a subseries from -m to n-1 if n is > m (position)
-m : -n   = will extract a subseries from -m to (-n-1)
               if -m > -n then empty series 
 m : -n   = will extract a subseries from m to (-n-1)

-m :


[100, 200, 300, 400, 100]


print(S1[-1:]) --    4    100
print(S1[-2:]) --    3    400
                     4    100
                     dtype: int64
print(S1[-3:]) --    2    300
                     3    400
                     4    100
                     dtype: int64
print(S1[-4:]) --    1    200
                     2    300
                     3    400
                     4    100
                     dtype: int64
print(S1[-12:]) --   0    100
                     1    200
                     2    300
                     3    400
                     4    100
                     dtype: int64

-m : n         


[100, 200, 300, 400, 100]


print(S1[-3:5]) --    2    300
                      3    400
                      4    100

print(S1[-3:4]) --    2     300
                        3     400
print(S1[-3:3]) --      2       300         

print(S1[-3:2]) --    Series([], dtype: int64)

print(S1[-12:3]) --   0    100

print(S1[-12:4]) --
0    100
1    200
2    300
3    400
dtype: int64
print(S1[-1:6])  -- 
The data is/are  



-m : -n

[100, 200, 300, 400, 100]

-1 -1 = -2
print(S1[-3:-1]) --    2    300
                       3    400
print(S1[-3:-6]) --    Series([], dtype: int64)
start with -3 nd reach upto -7
print(S1[-6:-3]) -- -6 to -4
0    100
1    200

print(S1[-5:-3]) -- -5 to -4
0    100
1    200
print(S1[-12:-5]) --   -12 to -6
Series([], dtype: int64)

print(S1[-12:-4]) --   0    100

print(S1[-12:-2]) --  -12 to -3
0    100
1    200
2    300

print(S1[-12:-1]) --  -12 to -2
0    100
1    200
2    300
3    400

** When -m < -n  then the last element can't be extracted. (cause -1-1 = -2)

Q. Write the statement to show all the elements of a series other than the last    one using negative positional indexing.
0      1     2     3
['A', 'B',  'C',  'D']
-4    -3    -2    -1

0    A
1    B
2    C

S1[-4:-1] -- -4 TO -2
S1[0:3] -- 0 TO 2ND INDEX ADDRESS
S1[-5:-1] -- -5 TO -2
[0:-1] 
[-4:3] 

print(S1[-12:0]) --    Series([], dtype: int64) 

m : -n 


print(S1[0:-1]) -- 
0    100
1    200
2    300
3    400
dtype: int64

print(S1[2:-2]) -- 2    300

print(S1[2:-3]) -- Series([], dtype: int64)

print(S1[4:-2]) -- Series([], dtype: int64)


Write all the code statements in which the entire series can be extracted -
print(S1[0:])
print(S1[0:last pos. index address + 1])
print(S1[0:pos index address beyond])
print(S1[beyond / last neg. po. index address: ])
print(S1[beyond / last neg. po. index address: last pos. pos. index address + 1])