Overview:
- The first ‘n’ rows of a DataFrame can be selected using the instance method head(). The parameter ‘n’ specifies the number of rows to be present in the returned DataFrame.
- Similarly, the tail() method of the DataFrame returns the last ‘n’ rows of a DataFrame as a separate DataFrame instance.
- The default value for parameter 'n' in both the head() and tail() method is 5.
- Similarly, to manipulate a pandas.series instance which is a one-dimensional container, the head() and tail() methods of the series class can be invoked.
Example – Retrieve first ‘n’ rows of a DataFrame using head() method:
# Example Python program that selects first 'n' rows of a DataFrame # using the instance method head() import pandas as pds
numRows2Select = 2; # List of tuples colorData = [("red", "blue", "green"), ("white", "grey", "black"), ("cyan", "purple", "brown")];
# Create a dataframe from a list of tuples dataFrame = pds.DataFrame(colorData);
# Select first two rows of a DataFrame firstFive = dataFrame.head(numRows2Select);
# Print the new DataFrame print("First %d rows of a DataFrame selected using head():"%numRows2Select); print(firstFive); print(type(firstFive)); |
Output:
First 2 rows of a DataFrame selected using head(): 0 1 2 0 red blue green 1 white grey black <class 'pandas.core.frame.DataFrame'> |
Example – Retrieve last ‘n’ rows of a DataFrame using tail() method:
# Example Python program that selects last 'n' rows # from a pandas DataFrame instance import pandas as pds
# A list of tuples representing flight routes routes = [("Dallas, Frankfurt, Singapore"), ("New York, London, Singapore"), ("Chicago, London, Bangkok"), ("San Francisco, Paris, Hong Kong"), ("Phoenix, Amsterdam, Seoul")];
# Create a DataFrame object df = pds.DataFrame(routes, index=["R1", "R2", "R3", "R4", "R5"], columns=["Cities"]);
# Print the contents print("The contents of the DataFrame:"); print(df);
# Print only the last two rows print("Last 2 rows of the DataFrame selected using DataFrame.tail():"); print(df.tail(2)); |
Output:
The contents of the DataFrame: Cities R1 Dallas, Frankfurt, Singapore R2 New York, London, Singapore R3 Chicago, London, Bangkok R4 San Francisco, Paris, Hong Kong R5 Phoenix, Amsterdam, Seoul Last 2 rows of the DataFrame selected using DataFrame.tail(): Cities R4 San Francisco, Paris, Hong Kong R5 Phoenix, Amsterdam, Seoul |