Introduction
In this article, the main purpose is just to display five first data exist in a Pandas DataFrame. As it exist as part of the title of the DataFrame. Before going on to the actual execution of the command for getting five first data from a Pandas DataFrame variable, just look at the previous article. In this case, it is a previous article for giving information about how to use Pandas. Just access an article with the title of ‘How to Use Pandas’ in this link for using Pandas library. Moreover, an article with the title of ‘How to Use Pandas with DataFrame’ in this link for using DataFrame. Basically, it is a requirement in order to display the five first data from Pandas DataFrame. The first requirement is for having Pandas library. And the second one is for defining a variable with the DataFrame data structure.
How to Display Five First Data from Pandas DataFrame
After ensuring the requirements, below is the demonstration for showing how to get and display that five first data from a variable with the data type of Pandas DataFrame :
-
First of all, just run Command Prompt for the interface on executing the Python source code. Use any command line interface available in the device. In this context, it is using Command Prompt since the process will be in a device running using Microsoft Windows. Below is the appearance of the Command Prompt :
Microsoft Windows [Version 10.0.22000.856] (c) Microsoft Corporation. All rights reserved. C:\Users\Personal>
-
Next, type ‘python’ to get into the Python command console as follows :
Microsoft Windows [Version 10.0.22000.856] (c) Microsoft Corporation. All rights reserved. C:\Users\Personal>python Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
-
After that, import the Pandas library as follows :
Microsoft Windows [Version 10.0.22000.856] (c) Microsoft Corporation. All rights reserved. C:\Users\Personal>python Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import pandas as pd
-
In the next line, just define the variable with using DataFrame data structure as follows :
Microsoft Windows [Version 10.0.22000.856] (c) Microsoft Corporation. All rights reserved. C:\Users\Personal>python Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import pandas as pd >>> >>> import pandas as pd >>> pandas_dataframe = pd.DataFrame([1,2,3,4,5,6,7,8,9,10]) >>> print(pandas_dataframe.head()) 0 0 1 1 2 2 3 3 4 4 5 >>>
So, as in the above source code execution, there is a method which exist in the DataFrame variable with the name of ‘head()’ for listing or returning the first 5 (five) data available in the DataFrame.