[go: up one dir, main page]

0% found this document useful (0 votes)
27 views1 page

Series Functions

The document explains how to retrieve values from a pandas Series using methods such as head() and tail(). The head() method fetches the first n elements, while the tail() method retrieves the last n elements, with both defaulting to the top or bottom 5 rows if no value is specified. Additionally, the count() method is discussed, which returns the number of non-NaN values in a Series.

Uploaded by

Purvang Sunva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views1 page

Series Functions

The document explains how to retrieve values from a pandas Series using methods such as head() and tail(). The head() method fetches the first n elements, while the tail() method retrieves the last n elements, with both defaulting to the top or bottom 5 rows if no value is specified. Additionally, the count() method is discussed, which returns the number of non-NaN values in a Series.

Uploaded by

Purvang Sunva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

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 series.it 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=pd.Series(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
S.head()

Out[2]: 0 100
1 90
2 80
3 70
4 60
dtype: int64

In [3]: S.head(3)

Out[3]: 0 100
1 90
2 80
dtype: int64

In [4]: S.head(-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]: S.head(1:)
#We can not specify range of values in head function.

Cell In[5], line 1


S.head(1:)
^
SyntaxError: invalid syntax

In [ ]: 2-tail(n)-This method or function fetched last n elements


from a pandas series.it 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=pd.Series(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]: S.tail(4)

Out[7]: 5 50
6 40
7 30
8 20
dtype: int64

In [8]: S.tail(-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]: S.tail(1:)
#Note that with -ve argument tail function we can not
#extract values

Cell In[9], line 1


S.tail(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=pd.Series([x for x in[12,'12',np.nan,56.5]])
print('Printing Series st\n',st)
print('Total number of non-nan values\n',st.count())
#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=pd.Series([11,12,13])
print('Total number of elements in series\n',S.count())

Total number of elements in series


3

In [ ]:

You might also like