Skip to content Skip to sidebar Skip to footer

Apply Transformation Only On String Columns With Pandas, Ignoring Numeric Data

So, I have a pretty large dataframe with 85 columns and almost 90,000 rows and I wanted to use str.lower() in all of them. However, there are several columns containing numerical d

Solution 1:

From pandas 1.X you can efficiently select string-only columns using select_dtypes("string"):

string_dtypes = df.convert_dtypes().select_dtypes("string")
df[string_dtypes.columns] = string_dtypes.apply(lambda x: x.str.lower())

df
    A     B       C
0  10  john     dog
1  12  jack     cat
2  54  mary  monkey
3  23   bob   horse

df.dtypes

A     int64
B    string
C    string
dtype: object

This avoids operating on non-string data.


Solution 2:

Sure, select your str columns first using select_dtypes("object"):

df[df.select_dtypes("object").columns].applymap(str.lower)

Solution 3:

df.apply(lambda x:[x.str.lower() if x.dtypes==object else x])


Post a Comment for "Apply Transformation Only On String Columns With Pandas, Ignoring Numeric Data"