What are Attributes Pandas Dataframe

Pandas

Today in this article we are going to What are Attributes Pandas Dataframe object of pandas. The data frame object has many attributes that help us to determine the different data associated with the data frame. In this, we have the size, dimensions, values, etc.

So we will learn about all these attributes and we will see examples of each attribute to understand how they work. Let us begin with our tutorial:

Create a DataFrame


To start with our examples first of all we will create a Data Frame using pandas. Here we are creating a Data Frame.

import pandas as pd

df = pd.DataFrame({'Day':    [ 3,  29],
                   'Month': [9, 1],
                   'Year': [2019, 2020]})

print("The Data frame is:",df)

Output

The Data frame is:
     Day  Month  Year
0    3      9  2019
1   29      1  2020

List of Pandas Dataframe Attributes


  • DataFrame.index
  • DataFrame.columns
  • DataFrame.dtypes
  • DataFrame.values
  • DataFrame.axes
  • DataFrame.ndim
  • DataFrame.size
  • DataFrame.shape
  • DataFrame.empty

1. DataFrame.index attribute


DataFrame.index attribute is used to get the index of the DataFrame. index means the labels of the rows in the data frame. As you can see we have the index starting at 0 and goes to 1. Step size is 1 means increment by 1.

import pandas as pd

df = pd.DataFrame({'Day':    [ 3,  29],
                   'Month': [9, 1],
                   'Year': [2019, 2020]})
print("The Data frame index is:",df.index)

Output

The Data frame index is: RangeIndex(start=0, stop=2, step=1)

2. DataFrame.columns attribute


DataFrame.columns attribute is used to find the column labels of the DataFrame.In our example, the names of the columns are Day, Month, and Year.

import pandas as pd

df = pd.DataFrame({'Day':    [ 3,  29],
                   'Month': [9, 1],
                   'Year': [2019, 2020]})
print("The Data frame columns are:",df.columns)

Output

The Data frame columns are: Index(['Day', 'Month', 'Year'], dtype='object')

3. DataFrame.dtypes attribute


DataFrame.dtypes attribute returns the data type of the data in the Data Frame.As you can see from the output the data type of each column data is int64, so this attribute helps in determining the data types.

import pandas as pd

df = pd.DataFrame({'Day':    [ 3,  29],
                   'Month': [9, 1],
                   'Year': [2019, 2020]})
print("The Data frame dtypes:",df.dtypes)

Output

The Data frame dtypes: Day int64
Month int64
Year int64
dtype: object

4. DataFrame.values attribute


DataFrame.values attribute Returns a Numpy representation of the DataFrame. We can get all the values present in the data frame using this attribute.

import pandas as pd

df = pd.DataFrame({'Day':    [ 3,  29],
                   'Month': [9, 1],
                   'Year': [2019, 2020]})
print("The Data frame values are:",df.values)

Output

The Data frame values are: [[ 3 9 2019]
[ 29 1 2020]]

5. DataFrame.axes attribute


DataFrame.axes attribute Return a list representing the axes of the DataFrame. The axes are the rows and columns indexes. This attribute provides us both rows indexes as well as column indexes.

import pandas as pd

df = pd.DataFrame({'Day':    [ 3,  29],
                   'Month': [9, 1],
                   'Year': [2019, 2020]})
print("The Data frame axes are:",df.axes)

Output

The Data frame axes are: [RangeIndex(start=0, stop=2, step=1), Index(['Day', 'Month', 'Year'], dtype='object')]

6. DataFrame.ndim


DataFrame.ndim attributes Return an int representing the number of axes. This is like array dimensions.In our example, our data frame is 2 dimensional so this attribute is returning value of 2.

import pandas as pd

df = pd.DataFrame({'Day':    [ 3,  29],
                   'Month': [9, 1],
                   'Year': [2019, 2020]})
print("The Data frame dimensions are:",df.ndim)

Output

The Data frame dimensions are: 2

7. DataFrame.size attribute


DataFrame.size attribute Return an int representing the number of elements in this object. This means it returns the total number of elements in the data frame.

import pandas as pd

df = pd.DataFrame({'Day':    [ 3,  29],
                   'Month': [9, 1],
                   'Year': [2019, 2020]})
print("The Data frame size is:",df.size)

Output

The Data frame size is: 6

8.DataFrame.shape


DataFrame.shape attribute returns a tuple representing the dimensionality of the DataFrame.By Using this attribute We can get the shape of the data frame in terms of rows and columns present in it.

import pandas as pd

df = pd.DataFrame({'Day':    [ 3,  29],
                   'Month': [9, 1],
                   'Year': [2019, 2020]})
print("The Data frame shape is:",df.shape)

Output

The Data frame shape is: (2, 3)

9. DataFrame.empty attribute


DataFrame.empty helps us determine if the data frame is empty of not. We can use this attribute in conditional checking about the data frame and confirm if it is empty or has some data in it.


import pandas as pd

df = pd.DataFrame({'Day':    [ 3,  29],
                   'Month': [9, 1],
                   'Year': [2019, 2020]})print("Is Data frame empty :",df.empty)

Output

Is Data frame empty : False