Python Pandas: add_prefix() function

In pandas add prefix can be used for both series and dataframe.. It is used to prefix labels with string prefix. It is used as a row label of the given series object. In dataframe column, labels are prefixed.

Syntax

DataFrame.add_prefix(prefix)

Parameter:

prefix: The string which we can add before each label.

Return:

It may be series or dataframe with updated labels.

Example

import pandas as pd

df_0= pd.DataFrame({'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]})
df_0

Output:

 AB
015
126
237
348
df_0.add_prefix('a_')

Output:

 a_Aa_B
015
126
237
348