CBSE Class 11 & 12 Computer Science and Informatics Practices Python Materials, Video Lecture

Pandas Series provides two very useful methods for extracting the data from the top and bottom of the Series Object. These methods are head() and tail().

head() method is used to get the elements from the top of the series. By default, it gives 5 elements.

Syntax:
<Series Object> . head(n = 5)

Example:
Consider the following Series, we will perform the operations on the below given Series S.
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15``` ```import pandas as pd s = pd.Series({'A':1,'B':2,'C':3,'D':4,'E':5,'F':6,'G':7,'H':8}) print(s) Output: A 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 dtype: int64 ```

If we do not give any argument inside head() function, it will give by default 5 values from the top.
 ```1 2 3 4 5 6 7 8 9``` ```s.head() Output: A 1 B 2 C 3 D 4 E 5 dtype: int64 ```

### head() Function with Positive Argument

When a positive number is provided, the head() function will extract the top n rows from Series Object. In the below given example, I have given 7, so 7 rows from the top has been extracted.
 ``` 1 2 3 4 5 6 7 8 9 10 11``` ```s.head(7) Output: A 1 B 2 C 3 D 4 E 5 F 6 G 7 dtype: int64 ```

### head() Function with Negative Argument

We can also provide a negative value inside the head() function. For a negative value, it will check the index from the bottom and provide the data from the top.
 ```1 2 3 4 5``` ```s.head(-7) Output: A 1 dtype: int64 ```

## tail() Method

tail() method gives the elements of series from the bottom.

Syntax:
<Series Object> . tail(n = 5)

Example:
Consider the following Series, we will perform the operations on the below given Series S.
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15``` ```import pandas as pd s = pd.Series({'A':1,'B':2,'C':3,'D':4,'E':5,'F':6,'G':7,'H':8}) print(s) Output: A 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 dtype: int64 ```

### tail() function without argument

If we do not provide any argument tail() function gives be default 5 values from the bottom of the Series Object.
 ``` 1 2 3 4 5 6 7 8 9 10``` ```s.tail() ''' Output: D 4 E 5 F 6 G 7 H 8 dtype: int64 ''' ```

### tail() function Positive argument

When a positive number is provided tail() function given bottom n elements of the Series Object.
 ``` 1 2 3 4 5 6 7 8 9 10 11 12``` ```s.tail(7) ''' Output: B 2 C 3 D 4 E 5 F 6 G 7 H 8 dtype: int64 ''' ```

### tail() function Negative argument

When a negative number is provided it will give the data as follows:

 ```1 2 3 4 5 6``` ```s.tail(-7) ''' Output: H 8 dtype: int64 ''' ```