We wish to specify the function to be applied to the current column names to generate the desired names.
In this section, we cover the following function specification scenarios:
lambda
function to each column name. Passing an anonymous (lambda
) function which works similarly to a function but is often more convenient especially when parameters need to be passed.The examples we show here rename all columns of a data frame. See Column Selection for Implicit Renaming for how to apply the renaming function to a selected set of columns only.
We wish to rename columns by applying a named function to the current column names to generate the desired column names.
Built-In Function
In this example, we wish to convert the upper-case column names to lower-case.
df_2 = df.rename(columns=str.lower)
Here is how this works:
rename()
accepts a function to which the existing names of all columns are passed one by one.str.lower()
in the example above. See String Operations.Custom Function
In this example, we wish to have column names follow the template 'col_{col}_v2'
where {col}
is the current column name.
def format_name(p_col):
return 'col_{col}_v2'.format(col=p_col)
df_2 = df.rename(columns=format_name)
Here is how this works:
columns
argument of rename()
our custom function format_name()
.format()
to insert each current column name in the desired naming template. See String Interpolation.We wish to rename columns by applying a lambda
function to the current column names to generate the desired column names.
df_2 = df.rename(columns = lambda x: x.strip().lower().replace(' ', '_'))
Here is how this works:
rename()
accepts a lambda
function (an anonymous function) to which the existing names of all columns are passed one by one.lambda
function is expected to take a single string value as input (the existing column name) and return a single string value as output (the new column name).lambda
operates on one string at a time, we can’t use the Pandas vectorized string manipulation functions. Instead, we use the core Python string functions e.g. x.strip()
in this example. See String Operations.Alternative: Using Vectorized String Manipulation
df_2 = df\
.set_axis(df.columns.str.strip().str.lower().str.replace(" ", "_"),
axis=1)
Here is how this works:
set_axis()
expects a set of column names of the same length as columns. We set axis=1
to specify that we wish to change column names not row indices. See Set Names.Alternative: Set columns
Attribute
df.columns = df.columns.str.strip().str.lower().str.replace(" ", "_")
Here is how this works:
columns
attribute of a data frame.