Wednesday, 4 August 2021

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])



No comments:

Post a Comment