Objective is to convert each string to uppercase in Pandas dataframe (df).
Used following 3 approaches but not sure why 1st is NOT working ?
Please refer code snippet and 3 dataframes viz df1, df2, df3.
Using pandas.version == 2.1.1
import pandas as pd
# create a sample dataframe
df = pd.DataFrame({'C0': [1, 2, 3], 'C1': ['a', 'b', 'c']})
# apply lambda function to each element of the dataframe
df1 = df.apply(lambda x: x.upper() if isinstance(x, str) else x)
print(df1, '\n')
# convert all elements of the dataframe to strings and then convert them to uppercase
df2 = df.apply(lambda x: x.astype(str).str.upper())
print(df2, '\n')
# applymap lambda function to each element of the dataframe
df3 = df.applymap(lambda x: x.upper() if isinstance(x, str) else x)
print(df3, '\n')
C0 C1
0 1 a
1 2 b
2 3 c
C0 C1
0 1 A
1 2 B
2 3 C
C0 C1
0 1 A
1 2 B
2 3 C
- Why outputs differ in
df1anddf2? - Why we need to cast the type of each column into string as in
df2? - Why
apply()doesn't work indf1butapplymap()worked withdf3where it checks each column datatype as string withisinstance()
df.applyby default applies a function toaxis=0(to each column), where each column is of typepd.Series, so it does not pass the checkif isinstance(x, str)and remains as is.You don't actually have to, as you expect to apply changes to a distinct concrete column. Unless you expect to change multiple
strcolumns at once: