In this article, we will discuss the different attributes of a dataframe. Attributes are the properties of a DataFrame that can be used to fetch data or any information related to a particular dataframe.
The syntax of writing an attribute is:
DataFrame_name.attribute
These are the attributes of the dataframe:
- index
- columns
- axes
- dtypes
- size
- shape
- ndim
- empty
- T
- values
index
There are two types of index in a DataFrame one is the row index and the other is the column index. The index attribute is used to display the row labels of a data frame object. The row labels can be of 0,1,2,3,… form and can be of names.
Syntax: dataframe_name.index
Example 1: When the index is not mentioned in a DataFrame
Python3
import pandas as pd
dict = { "Student" : [ "Arnav" , "Neha" ,
"Priya" , "Rahul" ],
"Marks" : [ 85 , 92 , 78 , 83 ],
"Sports" : [ "Cricket" , "Volleyball" ,
"Hockey" , "Badminton" ]}
df = pd.DataFrame( dict )
display(df)
print (df.index)
|
Output:
In this program, we have made a DataFrame from a 2D dictionary and then printed this DataFrame on the output screen and at the end of the program, we have implemented an index attribute (df.index) to print the index labels of this DataFrame. As we have not mentioned any index labels in this program, it will automatically take the index from 0 to n numbers where n is the number of rows and then printed on the output screen.
Example 2: When the index is mentioned in a DataFrame
Python3
import pandas as pd
dict = { "Student" : [ "Arnav" , "Neha" ,
"Priya" , "Rahul" ],
"Marks" : [ 85 , 92 , 78 , 83 ],
"Sports" : [ "Cricket" , "Volleyball" ,
"Hockey" , "Badminton" ]}
df = pd.DataFrame( dict , index = [ 'I' , 'II' , 'III' , 'IV' ])
display(df)
print (df.index)
|
Output:
In this program, we have made a DataFrame from a 2D dictionary and then print this DataFrame on the output screen and at the end of the program, we have implemented index attribute (df.index) to print the index labels of this DataFrame, as we have mentioned index labels in this program as I, II, III and IV, so it will print the same on the output screen.
columns
This attribute is used to fetch the label values for columns present in a particular data frame.
Syntax: dataframe_name.columns
Python3
import pandas as pd
dict = { "Sales" : { 'Name' : 'Shyam' ,
'Age' : 23 , 'Gender' : 'Male' },
"Marketing" : { 'Name' : 'Neha' ,
'Age' : 22 , 'Gender' : 'Female' }}
data_frame = pd.DataFrame( dict )
display(data_frame)
print (data_frame.columns)
|
Output:
In this program, we have made a DataFrame from a 2D dictionary having values as dictionary object and then printed this DataFrame on the output screen and at the end of the program, we have implemented column attribute as print(data_frame.columns) to print the column labels of this DataFrame. In this program, column labels are “Marketing and Sales” so it will print the same.
axes
This attribute is used when we want to fetch the values of all row labels and all column labels at a time.
Syntax: dataframe_name.axes
Python3
import pandas as pd
dict = { "Sales" : { 'Name' : 'Shyam' ,
'Age' : 23 , 'Gender' : 'Male' },
"Marketing" : { 'Name' : 'Neha' , 'Age' : 22 ,
'Gender' : 'Female' }}
data_frame = pd.DataFrame( dict )
display(data_frame)
print (data_frame.axes)
|
Output:
In this program, we have made a DataFrame from a 2D dictionary having values as dictionary object and then printed this DataFrame on the output screen At the end of the program, we have implemented axes attribute as a print(data_frame.axes) to print the column labels as well as row labels of this DataFrame.
dtypes
The purpose of this attribute is to display the data type for each column of a particular dataframe.
Syntax: dataframe_name.dtypes
Python3
import pandas as pd
dict = { "Sales" : { 'Name' : 'Shyam' ,
'Age' : 23 , 'Gender' : 'Male' },
"Marketing" : { 'Name' : 'Neha' ,
'Age' : 22 , 'Gender' : 'Female' }}
data_frame = pd.DataFrame( dict )
display(data_frame)
print (data_frame.dtypes)
print ( "..Another data frame.." )
dict2 = { "Student" : [ "Arnav" , "Neha" ,
"Priya" , "Rahul" ],
"Marks" : [ 85 , 92 , 78 , 83 ],
"Sports" : [ "Cricket" , "Volleyball" ,
"Hockey" , "Badminton" ]}
data_frame = pd.DataFrame(dict2)
display(data_frame)
print (data_frame.dtypes)
|
Output:
In this program, we have made two DataFrames from a 2D dictionary having values as dictionary object and then printed these DataFrames on the output screen. At the end of each DataFrame, we have implemented “dtypes” attribute as print(data_frame.dtypes) to print the data types of each column for both the DataFrame.
size
This attribute is used to display the total number of elements or items present in a data frame.
Syntax: dataframe_name.size
Python3
import pandas as pd
dict = { "Sales" : { 'Name' : 'Shyam' ,
'Age' : 23 , 'Gender' : 'Male' },
"Marketing" : { 'Name' : 'Neha' ,
'Age' : 22 , 'Gender' : 'Female' }}
data_frame = pd.DataFrame( dict )
display(data_frame)
print ( "The total number of elements are:" )
print (data_frame.size)
|
Output:
In this program, we have made a DataFrame from a 2D dictionary having values as dictionary object and then printed this DataFrame on the output screen. At the end of the program, we have implemented size attribute as print(data_frame.size) to print the total number of elements or items of this DataFrame. In this data frame, there is a total of 6 elements which 3 elements from the 1st column and 3 from the 2nd column.
shape
This attribute is used to display the total number of rows and columns of a particular data frame. For example, if we have 3 rows and 2 columns in a DataFrame then the shape will be (3,2).
Syntax: dataframe_name.shape
Python3
import pandas as pd
dict = { "Sales" : { 'Name' : 'Shyam' ,
'Age' : 23 , 'Gender' : 'Male' },
"Marketing" : { 'Name' : 'Neha' ,
'Age' : 22 , 'Gender' : 'Female' }}
data_frame = pd.DataFrame( dict )
display(data_frame)
print ( "Shape of the DataFrame:" )
print (data_frame.shape)
|
Output:
In this program, we have made a DataFrame from a 2D dictionary having values as dictionary object and then printed this DataFrame on the output screen At the end of the program, we have implemented shape attribute as print(data_frame.shape) to print the number of rows and columns of this DataFrame. In his DataFrame, there are 3 rows and 2 columns so it will print (3,2).
ndim
ndim means the number of dimensions and this attribute is used to display the number of dimensions of a particular data frame, and a DataFrame is of 2 Dimensional objects.
Syntax: dataframe_name.ndim
Python3
import pandas as pd
dict = { "Sales" : { 'Name' : 'Shyam' , 'Age' : 23 ,
'Gender' : 'Male' },
"Marketing" : { 'Name' : 'Neha' , 'Age' : 22 ,
'Gender' : 'Female' }}
data_frame = pd.DataFrame( dict )
display(data_frame)
print ( "Number of Dimensions:" )
print (data_frame.ndim)
|
Output:
In this program, we have made a DataFrame from a 2D dictionary having values as dictionary object and then printed this DataFrame on the output screen At the end of the program, we have implemented ndim attribute as print(data_frame.ndim) to print the number of dimensions of this DataFrame. As we know that a DataFrame is a 2 Dimensional object, so it will print 2.
empty
This attribute is used to check whether the data frame is empty or not. This attribute returns true if the data frame is empty and false if the DataFrame is not empty.
Syntax: dataframe_name.empty
Python3
import pandas as pd
dict = { "Sales" : { 'Name' : 'Shyam' ,
'Age' : 23 ,
'Gender' : 'Male' },
"Marketing" : { 'Name' : 'Neha' ,
'Age' : 22 ,
'Gender' : 'Female' }}
data_frame = pd.DataFrame( dict )
display(data_frame)
print ( "Is this DataFrame empty?" )
print (data_frame.empty)
print ( "..Another data frame.." )
dict2 = {}
data_frame = pd.DataFrame(dict2)
display(data_frame)
print ( "Is this DataFrame empty?" )
print (data_frame.empty)
|
Output:
In this program, we have made two DataFrames from a 2D dictionary having values as dictionary object and then printed these DataFrames on the output screen At the end of each DataFrame, we have implemented an “empty” attribute as print(data_frame.empty) to check whether any of the DataFrame is empty or not. In this program 1st, DataFrame is not empty so it will print “False” and the 2nd DataFrame is empty so it will print “True”.
T (Transpose)
This attribute is used to change the rows into columns and columns into rows.
Syntax: dataframe_name.T
Python3
import pandas as pd
dict = { "Sales" : { 'Name' : 'Shyam' ,
'Age' : 23 ,
'Gender' : 'Male' },
"Marketing" : { 'Name' : 'Neha' ,
'Age' : 22 ,
'Gender' : 'Female' }}
data_frame = pd.DataFrame( dict )
display(data_frame)
print ( "Transpose of this DataFrame is:" )
print (data_frame.T)
|
Output:
In this program, we have made a DataFrame from a 2D dictionary having values as dictionary object and then printed this DataFrame on the output screen At the end of the program, we have implemented “T” attribute as print(data_frame.T) to print the transpose of this DataFrame. Transpose means all rows of the DataFrame will be changed to columns and vice-versa.
values
This attribute is used to represent the values/data of dataframe in NumPy array form.
Syntax: dataframe_name.values
Python3
import pandas as pd
dict = { "Sales" : { 'Name' : 'Shyam' ,
'Age' : 23 ,
'Gender' : 'Male' },
"Marketing" : { 'Name' : 'Neha' ,
'Age' : 22 ,
'Gender' : 'Female' }}
data_frame = pd.DataFrame( dict )
display(data_frame)
print ( "NumPy Array form of this DataFrame is:" )
print (data_frame.values)
|
Output:
In this program, we have made a DataFrame from a 2D dictionary having values as dictionary object and then printed this DataFrame on the output screen At the end of the program, we have implemented the “values” attribute as print(data_frame.values) to print all the data of this DataFrame in the form of NumPy array.
Share your thoughts in the comments
Please Login to comment...