Count the Number of Pandas DataFrame Columns

  1. Count Number of Columns of Pandas dataframe Using the column Property
  2. Count Number of Columns of Pandas dataframe Using the shape Property
  3. Count Number of Columns of Pandas dataframe Using the Typecasting
  4. Count the Number of Columns of Pandas dataframe Using dataframe.info() Method

In pandas dataframe, data is stored or displayed in the tabular format like in rows and columns. Pandas help us in retrieving or counting the number of rows and columns in the dataframe by using various approaches.

We will explore various methods in this tutorial related to counting the number of columns of a Pandas dataframe.

Count Number of Columns of Pandas dataframe Using the column Property

Using the column property of the pandas dataframe, we can retrieve the column list and calculate column length and count the number of columns in the dataframe.

See the following example. First, we have created a dataframe of products. Using column_list = dataframe.columns we retrieved the list of columns and then using len(column_list) counted the number of columns.

Example Codes:

import pandas as pd
import numpy as np
from IPython.display import display

# creating a DataFrame
dict = {'Products' : ['Intel Dell Laptops', 'HP Laptops', 'Lenavo Laptops', 'Acer Laptops'],
        'Price dollar' : [350, 300, 400, 250 ],
        'Percentage Sale' : [83, 99, 84, 76]}
dataframe = pd.DataFrame(dict)
  
# displaying the DataFrame
display(dataframe)

# To get the list of columns of dataframe
column_list = dataframe.columns
  
# Printing Number of columns
print('Number of columns:', len(column_list))

Output:

pandas count number of columns - column property

Count Number of Columns of Pandas dataframe Using the shape Property

It retrieves the tuples to represent the DataFrame shape when using the shape property. In the following example, the line shape=dataframe.shape will return the dataframe shape and the shape[1] counts the number of columns.

Example Codes:

import pandas as pd
import numpy as np
from IPython.display import display

# creating a DataFrame
dict = {'Products' : ['Intel Dell Laptops', 'HP Laptops', 'Lenavo Laptops', 'Acer Laptops'],
        'Price dollar' : [350, 300, 400, 250 ],
        'Percentage Sale' : [83, 99, 84, 76],
        'quantity' : [10, 16, 90, 100]}
dataframe = pd.DataFrame(dict)
  
# displaying the DataFrame
display(dataframe)

# Get shape of the dataframe
shape = dataframe.shape

# Printing Number of columns
print('Number of columns :', shape[1])

Output:

pandas count number of columns - shape property

As we can see in the above output, it displays the total number of columns is 4 in the above example.

Count Number of Columns of Pandas dataframe Using the Typecasting

We use the typecasting approach in this method, which is almost similar to the column property. When we use typecasting to the dataframe list, it retrieves the list of column names. See the following example for more understanding about the typecasting approach:

Example Code:

import pandas as pd
import numpy as np
from IPython.display import display

# creating a DataFrame
dict = {'Products' : ['Intel Dell Laptops', 'HP Laptops', 'Lenavo Laptops', 'Acer Laptops'],
        'Price dollar' : [350, 300, 400, 250 ],
        'Percentage Sale' : [83, 99, 84, 76],
        'quantity' : [10, 16, 90, 100]}
dataframe = pd.DataFrame(dict)
  
# displaying the DataFrame
display(dataframe)

# Typecasting dataframe to list
dataframe_list = list(dataframe)
  
# Printing Number of columns
print('Number of columns :', len(dataframe_list))

Output:

pandas count number of columns - Typecasting

Count the Number of Columns of Pandas dataframe Using dataframe.info() Method

Using the info() method, we can print the complete concise summary of the pandas dataframe. In the following example, we used dataframe.info() at the end of the source code. It displays the information related to dataframe class, dtypes, memory usage, count number of columns, and ranges index.

Example Codes:

import pandas as pd
import numpy as np
from IPython.display import display

# creating a DataFrame
dict = {'Products' : ['Intel Dell Laptops', 'HP Laptops', 'Lenavo Laptops', 'Acer Laptops'],
        'Price dollar' : [350, 300, 400, 250 ],
        'Percentage Sale' : [83, 99, 84, 76],
        'quantity' : [10, 16, 90, 100]}
dataframe = pd.DataFrame(dict)
  
# displaying the DataFrame
display(dataframe)

# Print dataframe information using info() method
dataframe.info()
   

Output:

pandas count number of columns - info method

In the above image, we can see the concise summary of the dataframe, including the columns count.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Pandas DataFrame

  • Get Pandas DataFrame Column Headers as a List
  • Delete Pandas DataFrame Column
  • Convert Pandas Column to Datetime
  • Convert a Float to an Integer in Pandas DataFrame