Skip to content
Advertisement

Applymap on all but one Pandas DataFrame?

I have a DataFrame df that looks like this:

JavaScript

I would like to change each element of each column except for the first to its corresponding integer ASCII code (i.e. “M” gets mapped to the integer 77, “A” gets mapped to 65, etc.).

I can achieve this result with the following:

JavaScript

Is there a better way to do this? There must be a better way to do this than by creating a new DataFrame. Perhaps a way to do applymap in-place on a subset of columns?

Advertisement

Answer

You can assign to selected columns:

JavaScript

Or:

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