Learn DataFrame Attributes In Python

Before reading this article, I will suggest you read “DataFrames in Python”. In that article, I have explained about the DataFrames and different ways of creating DataFrames in Python. Now, in this article, I am going to explain all the attributes of a DataFrame.
 

DataFrame Attributes

 
All information related to a DataFrame is available in its attributes. Python provides a lot of dataframe attributes to access the information of a dataframe.
 
Some attributes are mentioned below.
 
DataFrame Attributes by Mahesh Verma
 
We can access all the information as below.
  1. <DataFrame Object> . <Attribute Name>  
Let's understand with an example. For this, first of all, we are creating a DataFrame. For creating a DataFrame, use the below code.
  1. import pandas  
  2. dictObj={‘EmpId’:[‘E01’,’E02’,’E03’],  
  3.               ‘EmpName’:[‘Raj,’Ram’,’Renu’],  
  4.               ‘Department’:[‘Accounts’,’HR’,’IT’]  
  5.               }  
  6. df=pandas.DataFrame(dictObj)    
We can create a DataFrame having the name df. To get all the information related to that data frame, we use dataframe attributes. Now, let’s go through all the dataframe attributes.
 

Get information related to Index, Columns, Axes and Data Types

 
index
 
Returns the starting value, ending value, and the difference(step) of row index. 
  1. df.index  
  2. ## Output is RangeIndex(start=0,stop=3,step=1)  
columns

Returns the column name of the dataframe.
  1. df.columns  
  2. ## Output : Index([‘EmpId’,’EmpName,’Department’],dtype=’object’)  
axes
 
Returns a list which contains the rowindex as well as the column name of the dataframe.
  1. df.axes  
  2. ## Output : [RangeIndex(start=0,stop=3,step=1), Index([‘EmpId’,’EmpName,’Department’],dtype=’object’)]  
dtypes
 
Returns datatypes of each column of a dataframe.
  1. df.dtypes  
  2.   
  3. ## Output :   
  4. ## EmpId        Object  
  5. ## EmpName  Object  
  6. ## Department   Object  
  7. ## dtype : Object  
See the output of all the above attributes in running mode,
 
index, columns, dtypes in DataFrame
 

Retrieving size (no. of elements), shape, number of dimensions

 
size
 
Returns a total number of elements present in dataframe.
  1. df.size  
  2. ## Output 12  
shape
 
Returns a tuple which gives the present number of rows and number of columns of a dataframe as an element.
  1. df.shape  
  2. ##Output : (3,3)  
ndim
 
Returns an integer value which represents the number of dimensions of a dataframe.
  1. df.ndim  
  2. ## Output : 2  
See the output of all the above attributes in running mode.

size, shape, ndim in DataFrame
 

Retrieving values

 
values
 
Returns a NumPy array which contains all rows as a value.
  1. df.values  
values in dataframe
 

Checking for emptiness

 
empty
 
Returns a Boolean value which represents if the dataframe is empty or not. If it will return “True”, then dataframe is empty, otherwise, it is not empty.
  1. df.empty  
  2. ##Output : False  
empty dataframe
 

Transposing a DataFrame

 
T
 
It transposes a dataframe, i.e., rows become columns and columns become rows.
  1. df.T  
transposing a dataframe
 

Getting Count of non-NA values in dataframe

 
count()
 
It will return non-NA values for each COLUMNS. By default, it will take 0 as an argument.
  1. df.count()  
  2. ## Output :  
  3. ## EmpId 3  
  4. ## EmpName 3  
  5. ## Department 3   
count(1)
 
If we pass 1 as an argument, then instead of returning number of columns, it will return number of each rows along with index number,
  1. df.count(1)  
  2. ## Output  
  3. ## 0 3  
  4. ## 1 3  
  5. ## 2 3  
Count with axis parameter
 
We can also explicitly specify an argument to count() as axis.
 
If we want to count columns value then pass argument like
  1. df.count(axis=’columns’)  
If we want to count rows value then pass argument like
  1. df.count(axix=’rows’)  
See the output of all the above count attribute in running mode,
 
count function in DataFrame
 

CONCLUSION

 
Now, we have learned about the DataFrames attributes in Python and how we can easily access any information of DataFrame.
 
All the queries related to this article and sample files are always welcome. Thanks for reading.!!!


Recommended Free Ebook
Similar Articles