Skip to content
Advertisement

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 data. Is there an easy solution for this?

JavaScript

Than, after using something like df.applymap(str.lower) I would get:

JavaScript

Currently it’s showing this error message:

JavaScript

Advertisement

Answer

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

JavaScript

This avoids operating on non-string data.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement