This article will demonstrate how to print or to display several first row of all columns using head function from a DataFrame variable. So, using jupyter notebook as a web-based application to run the script, import the data first. After finishing importing the data, store it into a DataFrame variable using Pandas library. Soon after that, select several first rows from the variable using the ‘head’ function. So, run the jupyter notebook first as in the following command execution :
(myenv) C:\python\data-science>jupyter notebook [I 17:08:28.062 NotebookApp] Serving notebooks from local directory: C:\python\data-science [I 17:08:28.062 NotebookApp] The Jupyter Notebook is running at: [I 17:08:28.067 NotebookApp] http://localhost:8888/?token=4dd9801ef2aacad1d445955b0ae4621b4c669da84c617e7b [I 17:08:28.068 NotebookApp] or http://127.0.0.1:8888/?token=4dd9801ef2aacad1d445955b0ae4621b4c669da84c617e7b [I 17:08:28.070 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [C 17:08:28.225 NotebookApp] To access the notebook, open this file in a browser: file:///C:/Users/Personal/AppData/Roaming/jupyter/runtime/nbserver-8796-open.html Or copy and paste one of these URLs: http://localhost:8888/?token=4dd9801ef2aacad1d445955b0ae4621b4c669da84c617e7b or http://127.0.0.1:8888/?token=4dd9801ef2aacad1d445955b0ae4621b4c669da84c617e7b
Following the execution of the Jupyter Notebook, the following are scripts to demonstrate the purpose :
1. Reading data from a CSV file with this script :
import pandas as pd df = pd.read_csv("nba-2.csv", index_col="Name")
2. Print the several first rows available in the DataFrame variable called ‘df’ using the following script :
df.head()
The following is the image output of the above execution :
As in the above image output, all the columns are available in the output display. But the rows in the output display is only five rows. So, the head function will actually display by default the first five rows available in the DataFrame variable called df. The function ‘head’ is actually a default function available in any variable with the type of DataFrame. Furthermore, in order to create the variable, Pandas library must be imported in the first place.