Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pandas

How to prefix the labels of a series object in pandas

Onyejiaku Theophilus Chidalu

Overview

The add_prefix() function in pandas is used to prefix the rows of a Series object.

A Series object is a one-dimensional data structure used to hold integers, strings, and other python objects.

Syntax

The add_prefix() function takes the syntax shown below:

DataFrame.add_prefix(str)
Syntax for the add_prefix() function

Parameter value

The add_prefix() function takes a single required parameter value, str, representing the string to be prefixed or added before each label.

Return value

The add_prefix() function returns a new dataframe with prefixed columns.

Example

# A code to illustrate the add_prefix() function in Pandas

# imporing the pandas library
import pandas as pd

# creating a dataframe
df = pd.Series([10, 20, 30, 40, 50])

print(df)

# adding prefixes to the columns of the dataframe
print(df.add_prefix("row_"))
Implementing the add_prefix() function

Explanation

  • Line 4: We import the pandas library.
  • Line 7: We create a dataframe called df.
  • Line 9: We print the df.
  • Line 12: We add the prefix row_ to the row labels of the df. We print the result to the console.

RELATED TAGS

pandas

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring