Built with Tutify.io

    AIStudyLabs

    Pandas Retrieve Number of Columns

    Pandas Retrieve Number of Columns

    To retrieve the number of columns in a Pandas DataFrame, you can use the shape attribute, which returns a tuple representing the dimensions of the DataFrame. The first element of the tuple is the number of rows, and the second element is the number of columns.

    In this article, I will explain how to retrieve the number of columns by using axes(), len(), shape(), and info() functions with examples.

    Key Points –

    Related: Get All Header Columns from pandas DataFrame

    <a id="h-quick-examples-of-retrieve-number-of-columns"></a>

    Quick Examples of Retrieve Number of Columns

    Following are quick examples of retrieving number columns.

    
    # Quick examples of retrieve number columns 
    
    # Using df.axes() method 
    # To get number columns
    cols = len(df.axes[1])
    count = str(cols)
    
    # Using DataFrame.len() method 
    count = len(df.columns)
    
    # Get the number of columns and rows
    shape = df.shape
    
    # Using DataFrame.shape method
    count = df.shape[1]
    
    # Using DataFrame.info() method 
    df.info()
    

    To run some examples of retrieving number columns, let’s create Pandas DataFrame using data from a dictionary.

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

    Yields below output.

    
    # 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
    

    <a id="h-retrieve-number-columns-using-dataframe-axes"></a>

    Retrieve Number Columns using DataFrame.axes()

    You can retrieve the number of columns in a Pandas DataFrame using the axes attribute. The axes attribute returns a list of axis labels, where the first element represents the row axis labels and the second element represents the column axis labels. To get the number of columns, you can use the len() function on the second element of the axes list.

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

    Yields below output.

    
    # Output:
    Get number of Columns: 4
    

    <a id="h-using-dataframe-len-method"></a>

    Using DataFrame.len() Method

    Alternatively, you can also retrieve the number of columns in a DataFrame using the len() function with the columns attribute.

    
    # Using DataFrame.len() method 
    count = len(df.columns)
    print("Get number of Columns:", count)
    
    # Output:
    # Get number of Columns: 4
    

    In the above examples, len(df.columns) gets you the number of columns. Here, the df.columns return the DataFrame column names as a list.

    <a id="h-using-dataframe-shape-method"></a>

    Using DataFrame.shape() Method

    Similarly, to retrieve the number of columns in a DataFrame using the shape method, you can access the second element of the tuple returned by shape, which represents the number of columns.

    
    # Get the number of columns and rows
    df.shape
    
    # Using DataFrame.shape[1]
    # To get columns count
    columns_count = df.shape[1]
    print("Get number of Columns:", columns_count)
    
    # Output:
    # Get number of Columns: 4
    

    <a id="h-using-dataframe-info-method"></a>

    Using DataFrame.info() Method

    The info() method in Pandas provides a concise summary of the DataFrame, including information about the number of columns. You can use it to quickly retrieve the number of columns.

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

    This method will display information about the DataFrame, including the number of columns and their data types, non-null values, and memory usage.

    
    # 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
    

    <a id="h-complete-example"></a>

    Complete Example

    
    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])
    count = str(cols)
    print("Get number of Columns: " + count)
    
    # Using DataFrame.len() method 
    count = len(df.columns)
    print("Get number of Columns:", count)
    
    # Using DataFrame.shape method
    count = df.shape[1]
    print("Get number of Columns:", count)
    
    # Get the number of columns and rows
    shape = df.shape
    print(shape)
    
    # Using DataFrame.info() method 
    df.info()
    

    <a id="h-frequently-asked-questions-on-retrieve-number-of-columns"></a>

    Frequently Asked Questions on Retrieve Number of Columns

    You can find the number of columns in a Pandas DataFrame using the shape attribute. The shape attribute returns a tuple where the second element represents the number of columns.

    You can use the len() function with the columns attribute of a Pandas DataFrame to find the number of columns. For example: num_columns = len(df.columns).

    The shape attribute returns a tuple representing the dimensions of the DataFrame. The first element is the number of rows, and the second element is the number of columns.

    The info() method provides a concise summary of the DataFrame, including information about the number of columns, their data types, non-null values, and memory usage. While it doesn't directly return the number of columns, it displays this information, making it easy to visually inspect.

    While the info() method provides a summary of the DataFrame, including the number of columns, it is not the most direct way to get the number of columns programmatically. The shape attribute or len(df.columns) is more straightforward.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, I have explained retrieve the number of columns from DataFrame using axes(), len(), shape() and info() functions with examples. When you use the shape, use the shape[1] to retrieve the number of columns.

    Happy Learning !!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References