In [ ]: Retrieving values from a Series using Methods of Series
The technical difference between Series attributes
and methods is that methods always end with parenthesis
In [ ]: 1-head(n)-This method or function fetched first n elements
from a pandas [Link] gives us the top 5 rows of data of
the series if no value for n is specified.
In [1]: import pandas as pd
S=[Link](range(100,10,-10))
S
Out[1]: 0 100
1 90
2 80
3 70
4 60
5 50
6 40
7 30
8 20
dtype: int64
In [2]: #Find the output of the following based on the above Series
[Link]()
Out[2]: 0 100
1 90
2 80
3 70
4 60
dtype: int64
In [3]: [Link](3)
Out[3]: 0 100
1 90
2 80
dtype: int64
In [4]: [Link](-3)
#Note when we pass -ve value in the head function
#it removes specified number of elements from bottom
Out[4]: 0 100
1 90
2 80
3 70
4 60
5 50
dtype: int64
In [5]: [Link](1:)
#We can not specify range of values in head function.
Cell In[5], line 1
[Link](1:)
^
SyntaxError: invalid syntax
In [ ]: 2-tail(n)-This method or function fetched last n elements
from a pandas [Link] gives us the last 5 rows of data of
the series if no value for n is specified.
In [6]: import pandas as pd
S=[Link](range(100,10,-10))
S
Out[6]: 0 100
1 90
2 80
3 70
4 60
5 50
6 40
7 30
8 20
dtype: int64
In [7]: [Link](4)
Out[7]: 5 50
6 40
7 30
8 20
dtype: int64
In [8]: [Link](-4)
#Note that with -ve value it removes 1st 4 values.
Out[8]: 4 60
5 50
6 40
7 30
8 20
dtype: int64
In [9]: [Link](1:)
#Note that with -ve argument tail function we can not
#extract values
Cell In[9], line 1
[Link](1:)
^
SyntaxError: invalid syntax
In [ ]: 3-count()-This function or method returns the number of
non-NaN values of a given series.
In [12]: import pandas as pd
import numpy as np
st=[Link]([x for x in[12,'12',[Link],56.5]])
print('Printing Series st\n',st)
print('Total number of non-nan values\n',[Link]())
#Note that dtype is object as 12 is enclosed with in quotes
Printing Series st
0 12
1 12
2 NaN
3 56.5
dtype: object
Total number of non-nan values
3
In [15]: #use of count with a Series with no nan values
import pandas as pd
S=[Link]([11,12,13])
print('Total number of elements in series\n',[Link]())
Total number of elements in series
3
In [ ]: