Dataframe Attributes In PANDAS

DATAFRAME ATTRIBUTES

 
In my previous article, we learned what a DataFrame is and how to create one. Refer to my previous article.
 
Now we will move onto "Attributes of DataFrame", which are useful when we want to fetch information related to a particular DataFrame.
 
We have 10 Attributes, which are used as follows.
 
SYNTAX
 
<DataFrameObject>. <attribute_name>
Let us understand all the attributes while considering the below DataFrame as an example,

INDEX

 
This attribute is used to fetch the index’s names, as the index could be 0,1,2,3 and so on, also it could be some names, as in our example, indexes are: English, Maths, Science, and French.
 
SYNTAX
 
<DataFrameObject>. <index>
  1. import pandas as pd    
  2.     
  3. dict= {'2018':[85,73,80,64], '2019':[60,80,58,96], '2020':[90,64,74,87] }    
  4.     
  5. df=pd.DataFrame(dict,index=['English','Math','Science','French'])    
  6. print(df.index)   
OUTPUT
 
Index(['English', 'Math', 'Science', 'French'], dtype='object')
 

COLUMNS

 
This attribute is used to fetch the column’s names, as in our case it should give column name as: 2018,2019, 2020
 
SYNTAX
 
<DataFrameObject>. <columns>
 
We use: print (df. columns)
 

AXES

 
This attribute is used to fetch both index and column names.
 
SYNTAX
 
<DataFrameObject>. <axes>
 
We use: print (df. axes)
  1. import pandas as pd      
  2.       
  3. dict= {'2018':[85,73,80,64], '2019':[60,80,58,96], '2020':[90,64,74,87] }      
  4.       
  5. df=pd.DataFrame(dict,index=['English','Math','Science','French'])      
  6. print("When we use 'Columns':")    
  7. print(df.columns)    
  8. print("\n")    
  9. print("When we use 'Axes':")    
  10. print(df.axes)   
OUTPUT
 
 

DTYPES

 
This attribute is used to fetch the data type values of the items in the DataFrame.
 
SYNTAX
 
<DataFrameObject>. <dtypes>
 
We use: print (df. dtypes)
  1. import pandas as pd      
  2.       
  3. dict= {'2018':[85,73,80,64], '2019':[60,80,58,96], '2020':[90,64,74,87] }      
  4.       
  5. df=pd.DataFrame(dict,index=['English','Math','Science','French'])      
  6. print(df.dtypes)    
OUTPUT
 
2018 int64
2019 int64
2020 int64
dtype: object
 

SIZE

 
This attribute is used to fetch the size of the DataFrame, which is the product of the number of rows and columns.
 
Here, in our example we have 4 rows and 3 columns, so 4*3 i.e. 12 is the size of our DataFrame.
 
SYNTAX
 
<DataFrameObject>. <size>
 
We use: print (df. size) 
 

SHAPE

 
This attribute also gives you the size but it also mentions its shape, i.e. the number of rows and number of columns
 
SYNTAX
 
<DataFrameObject>. <shape>
 
We use: print (df. shape) 
 

NDIM

 
This attribute is used to fetch the dimension of the given DataFrame. Like if it is 1-D, 2-D, or 3-D.
 
We are working on 2-D Data Structure.
 
SYNTAX
 
<DataFrameObject>. <ndim>
 
We use: print (df. ndim)
  1. import pandas as pd      
  2.       
  3. dict= {'2018':[85,73,80,64], '2019':[60,80,58,96], '2020':[90,64,74,87] }      
  4.       
  5. df=pd.DataFrame(dict,index=['English','Math','Science','French'])      
  6. print("Size of the DataFrame is:",df.size)    
  7. print("Shape of the DataFrame is:",df.shape)    
  8. print("Dimension of the DataFrame is:",df.ndim)    
OUTPUT
 
 

EMPTY

 
This attribute gives you a Boolean output in the form of true or false, by which we can find if there any emptiness of the DataFrame.
 
SYNTAX
 
<DataFrameObject>. <empty>
 
We have another attribute that can check the presence of NANs (Not a Number).
 
SYNTAX
 
<DataFrameObject>. <isna()>
  1. import pandas as pd      
  2. import numpy as np      
  3.       
  4. dict= {'2018':[85,73,80,64], '2019':[60,80,58,96], '2020':[90,60,74,87] }      
  5.       
  6. df1=pd.DataFrame(dict,index=['English','Math','Science','French'])      
  7. print("Using 'Empty' on Dataframe1:",df1.empty)    
  8. print('DataFrame is not Empty')      
  9. print('\n')      
  10. print('Finding NaN values... ','\n',df1.isna())      
  11. print('NOT FOUND!!')    
  12. print('\n')    
  13.       
  14. df2=pd.DataFrame(index=['English','Math','Science','French'])      
  15. print("Using 'Empty' on Dataframe2:")    
  16. print(df2.empty,'(DataFrame is Empty)')      
  17. print('\n')      
  18. print('Finding NaN values...',df2.isna())    
  19. print('FOUND!!')   
OUTPUT
 
 

COUNT

 
This attribute gives the count of the items in the DataFrame. By default, it gives the count of the rows.
 
We can set count (0) or count (1), 0 is for displaying the count of rows (this is by default) and 1 is for displaying the count of columns.
 
Instead, we can use axis='index' or axis=’columns’
 
SYNTAX
 
<DataFrameObject>. <count ()>
  1. import pandas as pd      
  2.       
  3. dict= {'2018':[85,73,80,64], '2019':[60,80,58,96], '2020':[90,64,74,87] }      
  4.       
  5. df=pd.DataFrame(dict,index=['English','Math','Science','French'])      
  6. print(df)      
  7. print('\n')    
  8. print("When we use 'count()':")    
  9. print(df.count())      
  10. print('\n')    
  11. print("When we use 'count(axis='index')':")    
  12. print(df.count(axis='index'))      
  13. print('\n')    
  14. print("When we use 'count(1)':")    
  15. print(df.count(1))      
  16. print('\n')    
  17. print("When we use 'count(axis='columns')':")    
  18. print(df.count(axis='columns'))   
OUTPUT
 
 

T

 
This attribute is used to transpose the DataFrame; i.e., rows becomes columns and columns become rows.
 
SYNTAX
 
<DataFrameObject>. <T>
  1. import pandas as pd      
  2.       
  3. dict= {'2018':[85,73,80,64], '2019':[60,80,58,96], '2020':[90,64,74,87] }      
  4.       
  5. df=pd.DataFrame(dict,index=['English','Math','Science','French'])      
  6. print(df)      
  7. print('\n')      
  8. df1=df.T    
  9. print("After Transpose:")    
  10. print(df1)   
OUTPUT
 
 

Summary

 
In this article, we learned about 10 dataframe attributes, their uses, and their implementation. Going forward you will become a pro in Pandas. Practice hard!
 
In my next article, we will learn about “How to Access data in DataFrames”.
 
Feedback or queries related to this article are most welcome.
 
Thanks for reading.


Similar Articles