Skip to main content

Python Pandas Lower/ Upper Case values in DataFrame Columns (Examples)

Lowercase / uppercase all column cell contents.

String type has methods for lower-casing and upper-casing.
A column is selected from dataframe and str (string) operations are made on it. But this would throw errors for int data types.
We can either skip over those columns using conditionals or use "astype" assert it as a string and then operate string methods on it without any error.Based on requirement one might choose to either skip over or handle all columns with mixed type contents .


Creating a new dataframe with dictionary (which has alpha numeric cell contents)


# importing pandas
import pandas as pd


# animal_data_with_alpha_nums dictionary
animal_data_with_alpha_nums = {
    "Name": ["Cat", "Dog", "Cow"],
    "Speed": [15, 12, 10],
    "Sound": ["Meow", "Woof", "Mooo"],
    "Alpha_Num":[123,"aBc123XyZ","123xYz"]
}

#creating a dataframe using the animal_data_with_alpha_nums dictionary
animal_with_alpha_nums_df = pd.DataFrame(animal_data_with_alpha_nums)

#printing animal_with_duplicates_df
print("animal_with_alpha_nums_df \n", animal_with_alpha_nums_df)
animal_with_alpha_nums_df Name Speed Sound Alpha_Num 0 Cat 15 Meow 123 1 Dog 12 Woof aBc123XyZ 2 Cow 10 Mooo 123xYz


Skipping the columns which have int data types


# get all column names
column_names = animal_with_alpha_nums_df.columns

lowercased_animals_df = pd.DataFrame()

uppercased_animals_df = pd.DataFrame()

skip_list = ["Speed","Alpha_Num"]

for column in column_names:

    if column in skip_list :
        #using the skip list to skip over columns which has int values
        #str methods will throw errors
        lowercased_animals_df[column] = animal_with_alpha_nums_df[column]
    
        uppercased_animals_df[column] = animal_with_alpha_nums_df[column]
        continue
    
    lowercased_animals_df[column] = animal_df[column].str.lower()
    
    uppercased_animals_df[column] = animal_df[column].str.upper()
    

print("lowercased_animals_df \n",lowercased_animals_df)

print("\n uppercased_animals_df \n",uppercased_animals_df)

lowercased_animals_df Name Speed Sound Alpha_Num 0 cat 15 meow 123 1 dog 12 woof aBc123XyZ 2 cow 10 mooo 123xYz uppercased_animals_df Name Speed Sound Alpha_Num 0 CAT 15 MEOW 123 1 DOG 12 WOOF aBc123XyZ 2 COW 10 MOOO 123xYz



Handle all columns with mixed datatypes by first setting type as string and then chainning string methos on it.


#handle alpha numeric / varied content types

# get all column names
column_names = animal_with_alpha_nums_df.columns

lowercased_animals_df = pd.DataFrame()

uppercased_animals_df = pd.DataFrame()


for column in column_names:

    #assert as string type and then use methods of "str" on it 
    #this will handle alphanumeric / mixed type values without throwing errors 
    
    uppercased_animals_df[column] = animal_with_alpha_nums_df[column].astype(str).str.upper()
    
    lowercased_animals_df[column] = animal_with_alpha_nums_df[column].astype(str).str.lower()

print("lowercased_animals_df \n",lowercased_animals_df)

print("\n uppercased_animals_df \n",uppercased_animals_df)

lowercased_animals_df Name Speed Sound Alpha_Num 0 cat 15 meow 123 1 dog 12 woof abc123xyz 2 cow 10 mooo 123xyz uppercased_animals_df Name Speed Sound Alpha_Num 0 CAT 15 MEOW 123 1 DOG 12 WOOF ABC123XYZ 2 COW 10 MOOO 123XYZ

Comments

Topics

Show more