Built with Tutify.io

    AIStudyLabs

    Convert Pandas Column to Lowercase

    Convert Pandas Column to Lowercase

    To convert a Pandas DataFrame column to lowercase, you can utilize methods like str.lower(), map(), apply(), or a lambda function. This method allows you to apply a string function directly to each element of the column. In this article, I will explain how to convert the entire contents of a column from uppercase to lowercase in a Pandas DataFrame.

    Key Points –

    <a id="h-quick-examples-of-convert-lowercase-column-values"></a>

    Quick Examples of Convert Lowercase Column Values

    Below are quick examples of converting lowercase column values in DataFrame.

    
    # Quick examples of convert lowercase column values
    
    # Example 1: convert lowercase column 
    # Using str.lower()
    df['Courses'] = df['Courses'].str.lower()
    
    # Example 2: convert lowercase column 
    # Using apply()
    df['Courses'] = df['Courses'].apply(str.lower)
    
    # Example 3: Use apply() & lambda function
    df['Courses'].apply(lambda x: x.lower())
    
    # Example 4: Convert pandas column 
    # To lowercase use map()
    df['Courses'] = df['Courses'].map(str.lower)
    

    To run some examples of converting pandas column to lowercase, let’s create Pandas DataFrame.

    
    import pandas as pd
    import numpy as np
    technologies= ({
        'Courses':["SPARK","PYSPARK","HADOOP","PANDAS"],
        'Fee' :[22000,25000,24000,26000],
        'Duration':['30days','50days','40days','60days'],
        'Discount':[1000,2300,2500,1400]
                  })
    df = pd.DataFrame(technologies)
    print("Create DataFrame:\n", df)
    

    Yields below output.

    <a id="h-pandas-convert-column-to-lowercase-using-str-lower"></a>

    Pandas Convert Column to Lowercase using str.lower()

    Pandas DataFrame column values can be converted to lowercase using the str.lower() function. This is a function from Series hence you can easily apply this function to a specific column. This syntax converts specified column values from uppercase to lowercase. Here we convert the column values and assign it back to the same column.

    
    # convert lowercase column use str.lower()
    df['Courses'] = df['Courses'].str.lower()
    print("Convert Pandas column to lower case:\n", df)
    

    Yields below output.

    <a id="h-convert-pandas-column-to-lowercase-using-apply"></a>

    Convert Pandas Column to Lowercase using apply()

    Alternatively, you can use the apply() function to convert a column in a Pandas DataFrame to lowercase. For instance, you use the apply() function to apply the str.lower() function to each element in the 'Courses' column of the DataFrame df. The str.lower() function is a built-in Python function that converts strings to lowercase.

    
    # convert lowercase column use apply()
    df['Courses'] = df['Courses'].apply(str.lower)
    print("Convert Pandas column to lower case:\n", df)
    
    # Output:
    # Convert Pandas column to lower case:
    #     Courses    Fee Duration  Discount
    # 0    spark  22000   30days      1000
    # 1  pyspark  25000   50days      2300
    # 2   hadoop  24000   40days      2500
    # 3   pandas  26000   60days      1400
    

    The syntax df['Courses']=df['Courses'].apply(str.lower) effectively changes uppercase column values to lowercase.

    <a id="h-using-apply-amp-lambda"></a>

    Using apply() & Lambda

    By utilizing the apply() function and passing a lambda function, specific column values in a DataFrame can be converted from uppercase to lowercase. In this approach, the lambda expression constructs an anonymous function.

    
    # Use apply() & lambda function
    df['Courses'].apply(lambda x: x.lower())
    print("Convert Pandas column to lower case:\n", df)
    
    # Output:
    # Convert Pandas column to lower case:
    #     Courses    Fee Duration  Discount
    # 0    spark  22000   30days      1000
    # 1  pyspark  25000   50days      2300
    # 2   hadoop  24000   40days      2500
    # 3   pandas  26000   60days      1400
    

    <a id="h-use-map-function"></a>

    Use map() Function

    Similarly, to convert column values in a given DataFrame from uppercase to lowercase, you can use the map() function. To achieve this, you pass the str.lower() function into the map() function and then call the specified column of the DataFrame. The syntax df['Courses']=df['Courses'].map(str.lower) effectively converts uppercase values in the column to lowercase.

    
    # Convert pandas column to lowercase use map()
    df['Courses'] = df['Courses'].map(str.lower)
    print("Convert Pandas column to lower case:\n", df)
    
    # Output:
    # Convert Pandas column to lower case:
    #     Courses    Fee Duration  Discount
    # 0    spark  22000   30days      1000
    # 1  pyspark  25000   50days      2300
    # 2   hadoop  24000   40days      2500
    # 3   pandas  26000   60days      1400
    

    <a id="h-complete-example-for-lowercase-column-values"></a>

    Complete Example For Lowercase Column Values

    
    import pandas as pd
    import numpy as np
    technologies= ({
        'Courses':["SPARK","PYSPARK","HADOOP","PANDAS"],
        'Fee' :['22000','25000','24000','26000'],
        'Duration':['30days','50days','40days','60days'],
        'Discount':['1000','2300','2500','1400']
                  })
    df = pd.DataFrame(technologies)
    print(df)
    
    # convert lowercase column use str.lower()
    df['Courses'] = df['Courses'].str.lower()
    print(df)
    
    # convert lowercase column use apply()
    df['Courses'] = df['Courses'].apply(str.lower)
    print(df)
    
    # Use apply() & lambda function
    df['Courses'].apply(lambda x: x.lower())
    print(df)
    
    # Convert pandas column to lowercase use map()
    df['Courses'] = df['Courses'].map(str.lower)
    print(df)
    

    <a id="h-faq-on-convert-pandas-column-to-lowercase"></a>

    FAQ on Convert Pandas Column to Lowercase

    To convert a single column to lowercase in a Pandas DataFrame, you can use the .str.lower() method.

    To convert multiple columns to lowercase at once in a Pandas DataFrame, you can use the .apply() method with a lambda function.

    When converting a column to lowercase in Pandas, the .str.lower() method handles missing values (NaN) gracefully by leaving them unchanged. You don't need any special handling—NaN values will remain NaN after the conversion.

    If your column contains a mix of strings and other data types (e.g., integers, floats, NaN), you should ensure that only string values are converted to lowercase to avoid errors. You can use a conditional check with .apply() to perform the conversion only on string data.

    Converting to lowercase using .str.lower() does not modify the original DataFrame in-place unless you explicitly assign the result back to the column. The .str.lower() method returns a new Series with the modified values, so you need to assign it back to the original column or create a new column to see the changes.

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

    Conclusion

    In this article, we explored various methods to convert specified column values in a DataFrame from uppercase to lowercase. By leveraging functions like str.lower(), map(), apply(), and lambda functions, we achieved this transformation efficiently.

    Happy Learning !!

    Related Articles

    References