Get Head and Tail of a Pandas Dataframe or Series

Get The Head And Tail Of A DataFrame Or Series

In this Python tutorial, we are going to discuss the different methods to get the head and tail of a pandas DataFrame or Series object. So let’s get started.


Why get the head and tail of a pandas DataFrame or Series?

We all know that Pandas is an essential Python library that is widely used for data analysis. And it is a well-known fact that data analysis deals with very large datasets. Hence to get a quick overview of the large sample dataset (loaded in the form of a pandas DataFrame or Series object), we need the head and tail of a pandas DataFrame or Series.

We mostly use the DataFrame.head() and DataFrame.tail() functions of the pandas DataFrame class to get the first and the last N rows (by default the value of this N = 5) of the pandas DataFrame or Series respectively.

The head and tail of a pandas DataFrame

So, let’s create a sample pandas DataFrame object before moving ahead with our discussion about the head and tail of a pandas DataFrame object.

Create a sample pandas DataFrame object

# Import pandas Python module
import pandas as pd

# Create a large pandas DataFrame object
df = pd.DataFrame({'RegNo': [111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125],
                   'Dept': ['ECE', 'ICE', 'IT', 'CSE', 'CHE', 'EE', 'ME', 'CSE', 'ICE', 'TT', 'ECE', 'IT', 'ME', 'BT', 'EE']})

# Print the created pandas DataFrame
print('Sample pandas DataFrame:\n')
print(df)

Output:

Sample pandas DataFrame:

    RegNo Dept
0     111  ECE
1     112  ICE
2     113   IT
3     114  CSE
4     115  CHE
5     116   EE
6     117   ME
7     118  CSE
8     119  ICE
9     120   TT
10    121  ECE
11    122   IT
12    123   ME
13    124   BT
14    125   EE

Get the head of a pandas DataFrame: pandas.DataFrame.head()

# Get the head of the sample pandas Series
print('First 10 rows of the sample pandas DataFrame:\n')
temp_df = df.head(10)
print(temp_df)

Output:

First 10 rows of the sample pandas DataFrame:

   RegNo Dept
0    111  ECE
1    112  ICE
2    113   IT
3    114  CSE
4    115  CHE
5    116   EE
6    117   ME
7    118  CSE
8    119  ICE
9    120   TT

Get the tail of a pandas DataFrame: pandas.DataFrame.tail()

# Get the tail of the sample pandas Series
print('Last 10 rows of the sample pandas DataFrame:\n')
temp_df = df.tail(10)
print(temp_df)

Output:

Last 10 rows of the sample pandas DataFrame:

    RegNo Dept
5     116   EE
6     117   ME
7     118  CSE
8     119  ICE
9     120   TT
10    121  ECE
11    122   IT
12    123   ME
13    124   BT
14    125   EE

Get the head and tail of a pandas DataFrame together: pandas.option_context()

# Get the head and tail of the sample pandas DataFrame
# Using the pd.option_context() function in Pandas
print('First and Last 5 rows of the sample pandas DataFrame:\n')
with pd.option_context('display.max_rows',10):
    print(df)

Output:

First and Last 5 rows of the sample pandas DataFrame:

    RegNo Dept
0     111  ECE
1     112  ICE
2     113   IT
3     114  CSE
4     115  CHE
..    ...  ...
10    121  ECE
11    122   IT
12    123   ME
13    124   BT
14    125   EE

[15 rows x 2 columns]

The head and tail of a pandas Series

So, let’s create a sample pandas Series object before moving ahead with our discussion about the head and tail of a pandas Series object.

Create a sample pandas Series object

# Import pandas Python module
import pandas as pd
# Import NumPy Python module
import numpy as np

# Create a pandas Series
sr = pd.Series(np.random.randn(1000))

# Print the created pandas Series
print('Sample pandas Series:\n')
print(sr)

Output:

Sample pandas Series:

0     -0.157775
1     -0.108095
2     -0.876501
3     -0.591994
4     -0.435755
         ...   
995    1.186127
996   -0.898278
997   -0.267392
998    1.295608
999   -2.024564
Length: 1000, dtype: float64

Get the head of a pandas Series: pandas.Series.head()

# Get the head of the sample pandas Series
print('First 10 values of the sample pandas Series:\n')
temp_sr = sr.head(10)
print(temp_sr)

Output:

First 10 values of the sample pandas Series:

0   -0.157775
1   -0.108095
2   -0.876501
3   -0.591994
4   -0.435755
5   -1.204434
6   -0.035977
7    0.015345
8   -0.453117
9   -0.695302
dtype: float64

Get the tail of a pandas Series: pandas.Series.tail()

# Get the tail of the sample pandas Series
print('Last 10 values of the sample pandas Series:\n')
temp_sr = sr.tail(10)
print(temp_sr)

Output:

Last 10 values of the sample pandas Series:

990   -0.239336
991   -1.475996
992   -0.162860
993    0.405505
994    0.458872
995    1.186127
996   -0.898278
997   -0.267392
998    1.295608
999   -2.024564
dtype: float64

Summing-up

In this Python tutorial, we have learned how to get the head and tail of a pandas DataFrame or Series using the head() and tail() functions. We have also seen how to get the head and tail of a pandas DataFrame simultaneously using the pandas.option_context() function In Pandas. Hope you have understood the things discussed above and are excited to use the pandas functions to get a quick overview of your large pandas DataFrame. Thanks for reading! Stay tuned with us for more learning resources on Python programming.