Built with Tutify.io

    AIStudyLabs

    How to Convert Pandas Uppercase Column

    How to Convert Pandas Uppercase Column

    To convert the values in a specific column of a pandas DataFrame to uppercase, you can use the str.upper(), map(), apply(), and lambda function. In this article, I will explain how to convert lowercase column values into uppercase column values of pandas DataFrame.

    Key Points –

    <a id="h-quick-examples-of-convert-pandas-uppercase-column"></a>

    Quick Examples of Convert Pandas Uppercase Column

    Following are quick examples of converting column values to uppercase in Pandas DataFrame.

    
    # Quick examples of convert pandas uppercase column
    
    # Example 1: Use str.upper() function 
    # to convert pandas column to uppercase
    df['Courses'] = df['Courses'].str.upper()
    
    # Example 2: Use apply() function
    # To convert pandas column to uppercase
    df['Courses'] = df['Courses'].apply(str.upper)
    
    # Example 3: Use apply() & lambda function
    df['Courses'].apply(lambda x: x.upper())
    
    # Example 4: Use map() function 
    # To convert column to uppercase
    df['Courses'] = df['Courses'].map(str.upper)
    

    <a id="h-usage-of-convert-pandas-uppercase-column"></a>

    Usage of Convert Pandas Uppercase Column

    To convert all values in a column of a Pandas DataFrame to uppercase, you can use the str.upper() string method. This is especially useful when you're cleaning or standardizing text data (e.g., names, categories, or product codes).

    First, let’s create a Pandas DataFrame.

    
    # convert pandas column to uppercase
    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)
    

    Yields below output.

    
    # Output:
       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-using-str-upper-to-convert-pandas-column-to-uppercase"></a>

    Using str.upper() to Convert Pandas Column to Uppercase

    You can use str.upper() method to convert DataFrame column values to uppercase. For that, you will call str.upper() function with a specified column of a given DataFrame. This syntax will convert specified column values from lowercase to uppercase.

    
    # Use str.upper() function 
    # To convert pandas column to uppercase
    df['Courses'] = df['Courses'].str.upper()
    print(df)
    
    # Output:
    #    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-apply-function-to-convert-pandas-column-to-uppercase"></a>

    Use apply() Function to Convert Pandas Column to Uppercase

    We can also use apply() function to convert column values of a given DataFrame to uppercase. For that, we need to pass str.upper() function into apply() function then, call the specified column of the given DataFrame. df['Courses']=df['Courses'].apply(str.upper) this syntax converts lowercase column values to uppercase column values.

    
    # Use apply() function 
    # To convert pandas column to uppercase
    df['Courses'] = df['Courses'].apply(str.upper)
    print(df)
    
    # Output:
    #    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-apply-amp-lambda-function"></a>

    Use apply() & Lambda Function

    Alternatively, we can pass lambda function into apply() function we can convert specified column values of a given DataFrame from lowercase to uppercase. Here, the lambda expression is used to construct an anonymous function.

    
    # Use apply() & lambda function
    df['Courses'].apply(lambda x: x.upper())
    print(df)
    
    # Output:
    #    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-to-convert-the-column-to-uppercase"></a>

    Use map() Function to Convert the Column to Uppercase

    We can use map() function to convert column values of a given DataFrame from lowercase to uppercase. For that, we need to pass str.upper() function into map() function then, call the specified column of the given DataFrame. df['Courses']=df['Courses'].map(str.upper) this syntax converts lowercase to uppercase column values.

    
    # Use map() function 
    # To convert column to uppercase
    df['Courses'] = df['Courses'].map(str.upper)
    print(df)
    
    # Output:
    #    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-convert-pandas-uppercase-column"></a>

    Complete Example For Convert Pandas Uppercase Column

    
    # Use map() function 
    # To convert column to uppercase
    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)
    
    # Use str.upper() function 
    # To convert pandas column to uppercase
    df['Courses'] = df['Courses'].str.upper()
    print(df)
    
    # Use apply() function 
    # To convert pandas column to uppercase
    df['Courses'] = df['Courses'].apply(str.upper)
    print(df)
    
    # Use apply() & lambda function
    df['Courses'].apply(lambda x: x.upper())
    print(df)
    
    # Use map() function 
    # To convert column to uppercase
    df['Courses'] = df['Courses'].map(str.upper)
    print(df)
    

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

    FAQ on Convert Pandas Uppercase Column

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

    You can convert multiple columns to uppercase simultaneously. Here's how to do it using the apply() method with str.upper()

    The str.upper() method will raise an error if applied to non-string data. Ensure the column contains string values or convert it first using astype(str).

    Use applymap() to apply upper() across the entire DataFrame if all columns are strings.

    To convert the column names to uppercase in a Pandas DataFrame, you can use the str.upper() method on the columns attribute.

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

    Conclusion

    In conclusion, this article has demonstrated various methods to convert the values of a specified column in a Pandas DataFrame from lowercase to uppercase. By using str.upper(), map(), apply(), and lambda methods, you can efficiently perform this transformation.

    Happy Learning !!

    Related Articles

    References