Articles

add prefix and suffix to column names pandas | add_prefix() & add_suffix() method

add prefix and suffix to column names pandas | add_prefix() & add_suffix() method


In this pandas tutorial, we will discuss about:

  • add prefix to column names pandas,
  • add suffix to column names pandas,

Lets learn a bit about pandas dataframe before diving into add prefix to column names pandas and add suffix to column names pandas.

 

Pandas DataFrame is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.

 

We can able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.

Syntax:

pandas.DataFrame(data)

Where, data is the input dataframe, The data can be a dictionary that stores list of values with specified key.

 

Example: Create dataframe

In this example, we will create a dataframe with 4 rows and 4 columns with college data and assign indices through index parameter.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['one','two','three','four'])

#display the dataframe
print(data)

Output: dataframe is created.

      college_id       college_name college_address Total Staff
one        c-001  vignan university          guntur        1200
two        c-021               vvit          guntur        3422
three      c-002           RVR - JC          guntur        5644
four       c-004  Andhra University          guntur         670

 

Lets now use this created dataframe to learn to add prefix to column names pandas and add suffix to column names pandas.


add prefix to column names pandas

We can add prefix to the column names in the pandas DataFrame by using add_prefix() method.

 

This method will add string to the first of the column name.

Syntax:

dataframe.add_prefix(string)

where,

  1. dataframe is the input dataframe
  2. string is the prefix added to the column

Example : pandas add prefix to all column names

In this pandas add prefix to all column names program, we are adding "column_" prefix to the entire dataframe.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['one','two','three','four'])

#add prefix - "column_" for all the columns
data.add_prefix("column_")

Output: pandas add prefix to all column names (column_)

add prefix to column names pandas

Now we have learned about pandas add prefix to all column names. Lets learn about add suffix to column names pandas.


add suffix to column names pandas

We can add suffix to the column names in the pandas DataFrame by using add_suffix() method.

 

This method will add string to the last of the column name.

Syntax:

dataframe.add_suffix(string)

where,

  1. dataframe is the input dataframe
  2. string is the suffix added to the column

Example : add suffix to column names pandas

In this add suffix to column names pandas program we are adding "_column" suffix to the entire dataframe.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['one','two','three','four'])

#add suffix - "_column" for all the columns
data.add_suffix("_column")

Outputadd suffix to column names pandas (_column) result

add suffix to column names pandas

Thus we have learned to add prefix to column names pandas and add suffix to column names pandas using add_prefix() & add_suffix() method.

 


Pandas

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Apr 20,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!