• Post author:
  • Post category:Pandas
  • Post last modified:May 16, 2024
  • Reading time:8 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

1. 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

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

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

5. 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

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)

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