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:

  A B
0 1 5
1 2 6
2 3 7
3 4 8
df_0.add_prefix('a_')

Output:

  a_A a_B
0 1 5
1 2 6
2 3 7
3 4 8
Tue, 11/16/2021 - 16:22

Authored by

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.