Getting the rows , columns, elements and dimensions of the DataFrame.
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,70,30],
'ENGLISH':[80,70,40,50,60,30]}
df = pd.DataFrame(data=my_dict)
print(df.shape)
Output
(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])
Output
Number of rows : 6
Number of columns : 4
size
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 )
Dimensions
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