• Post author:
  • Post category:Pandas
  • Post last modified:November 18, 2024
  • Reading time:13 mins read
You are currently viewing How to Get Pandas Columns Count

To count the number of columns in a pandas DataFrame, you can use various methods such as shape[], len(), list(), and info() methods. In this article, I will explain how to count the number of columns in a pandas DataFrame using different functions.

Advertisements

Key Points –

  • The .shape attribute returns a tuple (rows, columns) where the second element is the column count.
  • Applying len() to df.columns gives the count of columns directly.
  • Accessing df.columns.size provides the number of columns as an integer.
  • Calling df.columns.count() also returns the total number of columns.
  • The df.info() method provides an overview including the number of columns, along with column names and data types.

Quick Examples of Count Columns of DataFrame

Below are quick examples of how to count columns in Pandas DataFrame.


# Quick examples of count columns of dataframe

# Example 1:Pandas count columns 
# Using DataFrame.shape()
df2 = df.shape[1]

# Example 2: Pandas count columns and rows
df2 = df.shape

# Example 3: Pandas count columns 
# Using len()
df2 = len(df.columns)

# Example 4: Using columns property
col = df.columns
df2 = len(col)

# Example 5: Pandas count columns 
# Using list()
df_list = list(df)
df2 = len(df_list)

# Example 5: Using DataFrame.info() function
df2 = df.info()

To run some examples of getting pandas to count the number of columns, let’s create a Pandas DataFrame.


# Create Count Columns of DataFrame
import pandas as pd
import numpy as np
technologies= ({
    'Courses':["Spark","PySpark","Hadoop","Pandas"],
    'Fee': [22000,25000,30000,35000],
    'Duration':['30days','50days','40days','35days'],
    'Discount':[1000,2000,2500,1500]
              })
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
print(df)

Yields below output.


# Output:
    Courses    Fee Duration  Discount
r1    Spark  22000   30days      1000
r2  PySpark  25000   50days      2000
r3   Hadoop  30000   40days      2500
r4   Pandas  35000   35days      1500

Use DataFrame.shape() Function to Count Columns

Pandas DataFrame provides a shape property that returns the number of count columns and rows shape of the DataFrame in a tuple, where the shape[0] element is a row count and shape[1] is the columns count. Below is an example. To learn more about shape, refer to DataFrame.shape[]


# Pandas count columns 
# Using DataFrame.shape()
df2 = df.shape[1]
print(df2)

# Output:
# 4

# Pandas count columns and rows
df2 = df.shape
print(df2)

# Output:
# (4, 4)

Count Columns Using len()

To count the number of columns in a pandas DataFrame using the len() function, you can apply it to the .columns attribute of the DataFrame. The .columns attribute returns an Index object containing the column labels, and using len() on this Index object will give you the count of columns. For example len(df.columns) returns the number of columns in a DataFrame.


# Pandas count columns 
# Using len()
df2 = len(df.columns)
print(df2)

# Using columns property
col = df.columns
df2 = len(col)
print(df2)

# Output:
# 4

Count Columns Using list()

Alternatively, You can also use the list() with the combination of len() function to get the count of DataFrame columns. Here, list() takes the DataFrame as input and returns the data in a list.


# Pandas count columns 
# Using list()
df_list = list(df)
df2 = len(df_list)
print(df2)

# Output:
# 4

Using Pandas DataFrame.info() Function

The Pandas DataFrame.info() function offers comprehensive details about the DataFrame, encompassing column data types, index information, memory consumption, column count, and more.


# Using DataFrame.info() function
df2 = df.info()
print(df2)

Yields below output.


# Output:
Index: 4 entries, r1 to r4
Data columns (total 4 columns):

#   Column    Non-Null Count  Dtype 
---  ------    --------------  ----- 
 0   Courses   4 non-null      object
 1   Fee       4 non-null      int64 
 2   Duration  4 non-null      object
 3   Discount  4 non-null      int64 
dtypes: int64(2), object(2)
memory usage: 160.0+ bytes
None

Complete Example For Count Columns


import pandas as pd
import numpy as np
technologies= ({
    'Courses':["Spark","PySpark","Hadoop","Pandas"],
    'Fee': [22000,25000,30000,35000],
    'Duration':['30days','50days','40days','35days'],
    'Discount':[1000,2000,2500,1500]
              })
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
print(df)

# Pandas count columns 
# Using DataFrame.shape()
df2 = df.shape[1]
print(df2)

# Pandas count columns and rows
df2 = df.shape
print(df2)

# Pandas count columns 
# Using len()
df2 = len(df.columns)
print(df2)

# Using columns property
col = df.columns
df2 = len(col)
print(df2)

# Pandas count columns
# Using list()
df_list = list(df)
df2 = len(df_list)
print(df2)

# Using DataFrame.info() function
df2 = df.info()
print(df2)

FAQ on Get Pandas Columns Count

How can I get the number of columns in a Pandas DataFrame?

You can get the number of columns in a DataFrame using the shape attribute. The second value returned by shape represents the column count.

Is there a method to count columns directly in Pandas?

There is no dedicated method in Pandas specifically to count columns directly. However, you can easily get the number of columns using a few straightforward approaches.

Can I use the info() method to see the number of columns?

You can use the info() method to get an overview of the DataFrame, which includes the number of columns. When you call df.info(), the output provides.

Can I use describe() to determine the number of columns?

describe() provides a summary of numerical columns but doesn’t explicitly count columns. However, you can infer the count by checking the number of statistics displayed.

What if I want to count columns based on specific criteria?

You can use conditional filtering to count columns that meet certain criteria. For example, to count columns with numerical data types.

Conclusion

In conclusion, understanding how to count columns in a Pandas DataFrame is essential for data analysis and manipulation tasks. In this article, we explored several approaches to achieve this, including using the shape[], len(), list(), and info() functions.

Happy Learning !!

Related Articles

References