Pandas – Retrieve Number of Columns From DataFrame

Use axes(),len(),shape() and info() method to retrieve the number of columns from pandas DataFrame. In this article, I will explain how to retrieve the number of columns from pandas DataFrame with examples.

Related: Get All Header Columns from pandas DataFrame

1. Quick Examples of Retrieve Number Columns From DataFrame

If you are in a hurry, below are some quick examples of how to retrieve number columns from pandas DataFrame.


# Below are quick example
# Using df.axes() method to get number columns
cols = len(df.axes[1])
df2 = str(cols)

Using DataFrame.len() method 
df2 = len(df.columns)

# Using DataFrame.shape method
df2 = df.shape[1]

# Get the number of columns and rows
df2 = df.shape

# Using DataFrame.info() method 
df.info()

Now, let’s create a DataFrame with a few rows and columns, execute these examples and validate results. Our DataFrame contains column names Courses, Fee, Duration, and Discount.


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas","Hadoop"],
    'Fee' :[20000,25000,22000,24000,30000],
    'Duration':['30days','40days','35days','60days','50days'],
    'Discount':[1000,2300,2500,2000,3000]
              }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame(technologies,index=index_labels)
print(df)

Yields below output.


    Courses    Fee Duration  Discount
r1    Spark  20000   30days      1000
r2  PySpark  25000   40days      2300
r3   Python  22000   35days      2500
r4   pandas  24000   60days      2000
r5   Hadoop  30000   50days      3000

2. Using DataFrame.axes() Method to Retrieve Number Columns

Use DataFrame.axes() method in pandas allow to retrieve the number of columns. It accepts the argument ‘1’ for columns and ‘0’ for rows. For instance, len(df.axes[1]) to returns the number of columns.


# Using df.axes() method to get number columns
cols = len(df.axes[1])
df2 = str(cols)
print("Get number of Columns: " + df2)

Yields below output.


Get number of Columns: 4

3. Using DataFrame.len() Method to Retrieve Number Columns

You can also use DataFrame.len() method to retrieve a list of the columns in a pandas DataFrame. For example len(df.columns) gets you number of columns.


Using DataFrame.len() method 
df2 = len(df.columns)
print("Get number of Columns:", df2)

Yields same output as above.

4. Retrieve Number Columns to Using DataFrame.shape() Method

pandas DataFrame also provides shape property that returns the number of columns and rows shape of the DataFrame in a tuple, where the shape[0] element is a number of rows and shape[1] is the number of columns. For example df.shape[1] to returns the number of columns.


# Using DataFrame.shape method
df2 = df.shape[1]
print("Get number of Columns:", df2)

# Get the number of columns and rows
df2 = df.shape
print(df2)

Yields same output as above.

5. Using DataFrame.info() Method

DataFrame.info() method provides information about the DataFrame including dtype of columns and index, memory usage, number of columns, etc.


# Using DataFrame.info() method 
df.info()

Yields below output.


<class 'pandas.core.frame.DataFrame'>
Index: 5 entries, r1 to r5
Data columns (total 4 columns):
 #   Column    Non-Null Count  Dtype 
---  ------    --------------  ----- 
 0   Courses   5 non-null      object
 1   Fee       5 non-null      int64 
 2   Duration  5 non-null      object
 3   Discount  5 non-null      int64 
dtypes: int64(2), object(2)
memory usage: 200.0+ bytes

6. Complete Example For Retrieve Number Columns From DataFrame


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas","Hadoop"],
    'Fee' :[20000,25000,22000,24000,30000],
    'Duration':['30days','40days','35days','60days','50days'],
    'Discount':[1000,2300,2500,2000,3000]
              }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame(technologies,index=index_labels)
print(df)

# Using df.axes() method to get number columns
cols = len(df.axes[1])
df2 = str(cols)
print("Get number of Columns: " + df2)

Using DataFrame.len() method 
df2 = len(df.columns)
print("Get number of Columns:", df2)

# Using DataFrame.shape method
df2 = df.shape[1]
print("Get number of Columns:", df2)

# Get the number of columns and rows
df2 = df.shape
print(df2)

# Using DataFrame.info() method 
df.info()

Conclusion

In this article, you have learned how to retrieve the number of columns from DataFrame using DataFrame.axes(), DataFrame.len(),DataFrame.shape() and DataFrame.info() method with examples.

Happy Learning !!

You May Also Like

References

Leave a Reply

Pandas – Retrieve Number of Columns From DataFrame