DataFrame.shape , size , ndim


Getting the rows , columns, elements and dimensions of the DataFrame.
import pandas as pd 
df = pd.DataFrame(data=my_dict)
(6, 4)
As we are getting one tuple as output so we can display the elements to show number of rows ( first elemetn ) and columns ( second element )
print("Number of rows : ",df.shape[0])
print("Number of columns : ",df.shape[1])
   Number of rows :  6
Number of columns :  4


We will get total number of elements by using size. Here the output is 24.
print(df.size) # 24 
In above code check that the size returns the same value as returned by multiplication of elements returned by shape ( size = rows x columns )


We can read the dimension of the DataFrame.
print(df.ndim) # 2
print(df['NAME'].ndim) # 1 

Using Excel file

Create DataFrame from Excel file by using read_excel() and then get the shape , size and dimensions.
import pandas as pd 
df = pd.read_excel('D:\student.xlsx')

Using CSV file

Reading from csv file to create DataFrame
import pandas as pd 
df= pd.read_csv('D:\\my_data\\student.csv') # DataFrame from csv file data
Display the shape, size and ndim
print(df.shape) #  (35,5) ( rows, columns)
print("Number of rows : ",df.shape[0])
print("Number of columns : ",df.shape[1])
print(df.size) #  175
print(df.ndim) # 2
print(df['name'].ndim) # 1 
Sample student DataFrame
Pandas tail() head() read_csv() read_excel() to_excel()
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    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 All rights reserved worldwide Privacy Policy Disclaimer