DataFrame.head()

Pandas

DataFrame.tail(n=5)
Returns the last n rows of the DataFrame.
n : int, defalut is 5, number of rows to return.
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
         'ID':[1,2,3,4,5,6],
         'MATH':[80,40,70,70,60,30],
         'ENGLISH':[80,70,40,50,60,30]}
df = pd.DataFrame(data=my_dict) # dataframe 
print(df.tail()) # default, last 5 records
Output ( by default we get last 5 rows )
   NAME  ID  MATH  ENGLISH
1  Raju   2    40       70
2  Alex   3    70       40
3   Ron   4    70       50
4  King   5    60       60
5  Jack   6    30       30
Let us change the last line only by asking 2 records
print(df.tail(2))
Output
   NAME  ID  MATH  ENGLISH
4  King   5    60       60
5  Jack   6    30       30

Negative value

print(df.tail(-1)) # All records after first records
Reading from csv file to create DataFrame and displaying last n records.
import pandas as pd 
df= pd.read_csv('D:\\my_data\\student.csv') # DataFrame from csv file data
print(df.tail(2)) #  last 2 records
By using shape() you can get a tuple showing rows and columns. Create DataFrame from Excel file by using read_excel()
import pandas as pd 
df = pd.read_excel('D:\student.xlsx',index_col='id')
print(df.shape)
Output
(35, 4)
You can check the code and output at Exercise 1

str.contains.sum()

Using tail() output as string

Using to_string() we can convert the object to string and then add.
We used str() to convert tuple element as integer to string before adding.
str1="Rows:" + str(df.shape[0])+ ",Columns:"+str(df.shape[1])
str1=str1+ "\n"+df.head(2).to_string()
Download Sample student DataFrame or CSV or Excel file

Pandas head() read_csv() read_excel() to_excel()
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer