w3resource

Pandas Series: head() function

Get the first n rows in Pandas series

The head() function is used to get the first n rows.

Syntax:

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

Parameters:

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

Returns: obj_head - same type as caller

Raises:same type as caller
The first 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 head image

Example - Viewing the first 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.head()

Output:

  animal
0	dog
1	bee
2	cat
3	lion
4	monkey

Example - Viewing the first 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.head(3)

Output:

  animal
0	dog
1	bee
2	cat

Previous: Subset initial periods of Pandas time series
Next: Get the row label of the maximum value in Pandas series



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/pandas/series/series-head.php