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. dict= {'2018':[85,73,80,64], '2019':[60,80,58,96], '2020':[90,64,74,87] }
  3. df=pd.DataFrame(dict,index=['English','Math','Science','French'])
  4. 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. dict= {'2018':[85,73,80,64], '2019':[60,80,58,96], '2020':[90,64,74,87] }
  3. df=pd.DataFrame(dict,index=['English','Math','Science','French'])
  4. print("When we use 'Columns':")
  5. print(df.columns)
  6. print("\n")
  7. print("When we use 'Axes':")
  8. 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. dict= {'2018':[85,73,80,64], '2019':[60,80,58,96], '2020':[90,64,74,87] }
  3. df=pd.DataFrame(dict,index=['English','Math','Science','French'])
  4. 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. dict= {'2018':[85,73,80,64], '2019':[60,80,58,96], '2020':[90,64,74,87] }
  3. df=pd.DataFrame(dict,index=['English','Math','Science','French'])
  4. print("Size of the DataFrame is:",df.size)
  5. print("Shape of the DataFrame is:",df.shape)
  6. 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. dict= {'2018':[85,73,80,64], '2019':[60,80,58,96], '2020':[90,60,74,87] }
  4. df1=pd.DataFrame(dict,index=['English','Math','Science','French'])
  5. print("Using 'Empty' on Dataframe1:",df1.empty)
  6. print('DataFrame is not Empty')
  7. print('\n')
  8. print('Finding NaN values... ','\n',df1.isna())
  9. print('NOT FOUND!!')
  10. print('\n')
  11. df2=pd.DataFrame(index=['English','Math','Science','French'])
  12. print("Using 'Empty' on Dataframe2:")
  13. print(df2.empty,'(DataFrame is Empty)')
  14. print('\n')
  15. print('Finding NaN values...',df2.isna())
  16. 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. dict= {'2018':[85,73,80,64], '2019':[60,80,58,96], '2020':[90,64,74,87] }
  3. df=pd.DataFrame(dict,index=['English','Math','Science','French'])
  4. print(df)
  5. print('\n')
  6. print("When we use 'count()':")
  7. print(df.count())
  8. print('\n')
  9. print("When we use 'count(axis='index')':")
  10. print(df.count(axis='index'))
  11. print('\n')
  12. print("When we use 'count(1)':")
  13. print(df.count(1))
  14. print('\n')
  15. print("When we use 'count(axis='columns')':")
  16. 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. dict= {'2018':[85,73,80,64], '2019':[60,80,58,96], '2020':[90,64,74,87] }
  3. df=pd.DataFrame(dict,index=['English','Math','Science','French'])
  4. print(df)
  5. print('\n')
  6. df1=df.T
  7. print("After Transpose:")
  8. 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.