w3resource

Pandas Series: tail() function

Series-tail() function

The tail() function is used to return the last n rows.

This function returns last n rows from the object based on position. It is useful for quickly verifying data, for example, after sorting or appending rows.

Syntax:

Series.tail(self, n=5)
Pandas Series tail image

Parameters:

Name Description Type/Default Value Required / Optional
n Number of rows to select. int<
Default Value: 5
Required

Returns: type of caller
The last n rows of the caller object.

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({'animal':['dog', 'bee', 'cat', 'lion',
                   'monkey', 'tiger', 'fox', 'wolf', 'zebra']})
df

Output:

 animal
0	dog
1	bee
2	cat
3	lion
4	monkey
5	tiger
6	fox
7	wolf
8	zebra
Pandas Series tail image

Example - Viewing the last 5 lines:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({'animal':['dog', 'bee', 'cat', 'lion',
                   'monkey', 'tiger', 'fox', 'wolf', 'zebra']})
df.tail()

Output:

 animal
4	monkey
5	tiger
6	fox
7	wolf
8	zebra

Example - Viewing the last n lines (three in this case):

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({'animal':['dog', 'bee', 'cat', 'lion',
                   'monkey', 'tiger', 'fox', 'wolf', 'zebra']})
df.tail(3)

Output:

 animal
6	fox
7	wolf
8	zebra

Previous: Series-take() function
Next: Series-truncate() function



Follow us on Facebook and Twitter for latest update.