How to Get Pandas Columns Count

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:December 12, 2022

Use shape[], len(), list() and info() function to count columns from pandas DataFrame. In this article, I will explain how to count the number of columns from DataFrame by using these functions with examples.

1. Quick Examples of Count Columns of DataFrame

If you are in a hurry, below are some quick examples of how to count columns in DataFrame.


# Below are quick example

# 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()

Now, Let’s create Pandas DataFrame using data from a Python dictionary, where the columns are CoursesFeeDuration and Discount.


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.


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

2. 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)

3. Use len() Function to Count Columns

You can also use len() function to count columns in a DataFrame. 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

4. Use list() to count columns

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

5. Using Pandas DataFrame.info() Function

Pandas DataFrame.info() function provides information about the DataFrame including dtype of columns, index, memory usage, count columns, etc.


# 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

6. 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)

7. Conclusion

In this article, I have explained how to count columns in pandas DataFrame using shape[], len(), list(), and info() functions with examples.

Happy Learning !!

Related Articles

References

Leave a Reply