Skip to content
Advertisement

Rename dataframe columns with a mapper function that takes parameters

How can I pass along parameters to a mapper function in pandas.DataFrame.rename? The example below uses a mapper function test. I want to change it’s behavior based on an additional parameter that I pass along.

JavaScript

In this example, the mapper function appends an "A" to each column name. I want the mapper not always to append an "A" but a character that I give as parameter in the function call. So my question is: how can I pass along additional parameters to the function test?

Advertisement

Answer

IIUC, you can use functools.partial:

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