Skip to main content

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
Submitted by devanshi.srivastava on November 16, 2021

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

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.