The add_prefix()
function in pandas is used to prefix the columns of a data frame.
The add_prefix()
function takes the syntax shown below:
add_prefix("Col_")
The add_prefix()
function takes a single required parameter value, str
, which represents the string to be prefixed or add before each label.
The add_prefix()
function returns a new DataFrame with prefixed columns.
# A code to illustrate the add_prefix() function in Pandas# imporing the pandas libraryimport pandas as pd# creating a dataframedf = pd.DataFrame({'Name': ["Theo", "John", "Mosh"], 'Height': [1.83, 1.80, 1.78]})print(df)# adding prefixes to the columns of the dataframeprint(df.add_prefix("Col_"))
pandas
library.df
.df
."col_"
to the columns of df
. We print the result to the console.