Python | Pandas dataframe.add_prefix()

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

Dataframe.add_prefix() function can be used with both series as well as dataframes.

  • For Series, the row labels are prefixed.
  • For DataFrame, the column labels are prefixed.
Syntax:  DataFrame.add_prefix(prefix)

Parameters:
prefix : string

Returns: with_prefix: type of caller

For link to CSV file Used in Code, click here



Example #1: Prefix col_ in each columns in the dataframe

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd
import pandas as pd
  
# Making data frame from the csv file
df = pd.read_csv("nba.csv")
  
# Printing the first 10 rows of the
# dataframe for visualization
df[:10]

chevron_right


filter_none

edit
close

play_arrow

link
brightness_4
code

# Using add_prefix() function 
# to add 'col_' in each column label
df = df.add_prefix('col_')
  
# Print the dataframe
df 

chevron_right


Output:

 

Example #2: Using add_prefix() with Series in pandas

add_prefix() alters the row index labels in the case of series.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd
import pandas as pd
  
# Creating a Series 
df = pd.Series([1, 2, 3, 4, 5, 10, 11, 21, 4])
  
# This will prefix 'Row_' in 
# each row of the series
df = df.add_prefix('Row_')
  
# Print the Series
df

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.