Page 263 - AI Computer 10
P. 263
Now, let us learn how to use head() function in the dataframes. The head() function is used to print the top 5
rows of a list. The basic syntax of using head() function is:
import pandas as pd
df = pd.read_excel(r’F:\Worksheet_Cyber.xlsx’)
print(df.head())
The output of the above code is shown below:
Suppose, you have an Excel sheet that contains 100 rows of data and you want to print only 20 rows. In such
a case, you can pass the number of rows as a parameter in the head() function. The basic syntax to print the
desired number of rows among the large datasets is:
print(dataframe.head(20))
Let us understand the concept of parametrised head function with the help of an example given below:
import pandas as pd
df = pd.read_excel(r’F:\Worksheet_Cyber.xlsx’)
print(df.head(20))
The output of the above code is as follows:
Now, you want to sort the list of items according to the price. To do so, you can write the following code:
df = pd.read_excel(r’F:\Book_list.xlsx’)
df = df.sort_values(by=’Price’,ascending = True)
print(df.head())
The output of the above code is shown in the snapshot given below:
129
129