adding prefix to pandas column

  • Last Update :
  • Techknowledgy :

For Series, the row labels are prefixed. For DataFrame, the column labels are prefixed.,The add_prefix() function is used to prefix labels with string prefix.,Previous: Replace values in Pandas Series Next: Suffix labels with string suffix in Pandas series,Returns: Series or DataFrame New Series or DataFrame with updated labels.

Syntax:

Series.add_prefix(self, prefix)

Suggestion : 2

For Series, the row labels are prefixed. For DataFrame, the column labels are prefixed.,The string to add before each label.,Prefix labels with string prefix.,Suffix column labels with string suffix.

>>> s = pd.Series([1, 2, 3, 4]) >>>
   s
0 1
1 2
2 3
3 4
dtype: int64
>>> s.add_prefix('item_')
item_0 1
item_1 2
item_2 3
item_3 4
dtype: int64
>>> df = pd.DataFrame({
      'A': [1, 2, 3, 4],
      'B': [3, 4, 5, 6]
   }) >>>
   df
A B
0 1 3
1 2 4
2 3 5
3 4 6
>>> df.add_prefix('col_')
col_A col_B
0 1 3
1 2 4
2 3 5
3 4 6

Suggestion : 3

You may use add_prefix in order to add a prefix to each column name in Pandas DataFrame:

df = df.add_prefix('my_prefix')

You can then create a DataFrame as follows:

import pandas as pd

data = {
   'Product': ['ABC', 'DDD', 'XYZ', 'AAA', 'CCC'],
   'Price': [750, 430, 980, 250, 620],
   'Discount': ['Yes', 'No', 'No', 'Yes', 'No']
}

df = pd.DataFrame(data, columns = ['Product', 'Price', 'Discount'])
print(df)

Once you run the code in Python, you’ll get the following DataFrame:

  Product Price Discount
  0 ABC 750 Yes
  1 DDD 430 No
  2 XYZ 980 No
  3 AAA 250 Yes
  4 CCC 620 No

So for our example, the complete Python code would look like this:

import pandas as pd

data = {
   'Product': ['ABC', 'DDD', 'XYZ', 'AAA', 'CCC'],
   'Price': [750, 430, 980, 250, 620],
   'Discount': ['Yes', 'No', 'No', 'Yes', 'No']
}

df = pd.DataFrame(data, columns = ['Product', 'Price', 'Discount'])
df = df.add_prefix('Sold_')
print(df)

As you may see, the ‘Sold_‘ prefix was added to each column:

  Sold_Product Sold_Price Sold_Discount
  0 ABC 750 Yes
  1 DDD 430 No
  2 XYZ 980 No
  3 AAA 250 Yes
  4 CCC 620 No

Suggestion : 4

To add a string before each column label of DataFrame in Pandas, call add_prefix() method on this DataFrame, and pass the prefix string as argument to add_prefix() method.,In this Pandas Tutorial, we learned how to add a string as prefix to column labels of DataFrame, using pandas DataFrame.add_prefix() method.,In the following program, we will take a DataFrame with column labels [‘1’, ‘2’]. We add prefix string ‘col_’ to the column labels of this DataFrame using DataFrame.add_prefix() method.,In this tutorial, we will learn how to add a string as prefix to column labels of DataFrame, with examples.

The syntax of pandas DataFrame.add_prefix() method is

DataFrame.add_prefix(prefix)

Example.py

import pandas as pd

df = pd.DataFrame({
   '1': [10, 20, 30],
   '2': [40, 50, 60]
})

prefix = 'col_'

result = df.add_prefix(prefix)
print(result)

Output

col_0 col_1
0 10 40
1 20 50
2 30 60

Suggestion : 5

Pandas add_prefix() method is a built-in method of DataFrame that is used to add a prefix to row label and columns label in Series and DataFrame respectively.  ,If we call this method on Series then it adds prefix labels with string prefix to row label similarly it adds prefix with columns if called on DataFrame. Prefix value is a string value that is concatenated with the previous label value.,Here we have a DataFrame created by using the NumPy library and we are using the add_prefix() method to append column labels. See the example here.,We can use DataFrame.add_prefix() method to add string prefix for column labels in DataFrame. See the example here. This example is similar to the above code, executed by using Jupyter notebook. If you are not familiar with Jupyter then see this code to understand.

Syntax

DataFrame.add_prefix(prefix)

We can use DataFrame.add_prefix() method to add string prefix for column labels in DataFrame. See the example here. This example is similar to the above code, executed by using Jupyter notebook. If you are not familiar with Jupyter then see this code to understand.

#
import pandas library
import pandas as pd
# Create DataFrame of data
df = pd.DataFrame({
   'A': [12, 13, 14, 15],
   'B': [20, 40, 50, 30],
   'C': [-100, 40, -10, 20]
})
# print dataframe
print(df)
print("\nDataFrame with updated labels...\n")
# Add prefix
df2 = df.add_prefix("label")
print(df2)

If we are working with Series and want to modify its label by adding some prefix string then use the add_prefix() method. It returns a Series after modifying its labels. See the example here.

#
import pandas library
import pandas as pd
# Create Series of data
sr = pd.Series([12, 13, 14, 15])
# print Series
print(sr)
print("\nSeries with updated labels...\n")
# Add prefix
sr2 = sr.add_prefix("label")
print(sr2)

Suggestion : 6

Last Updated : 16 Nov, 2018

  • 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

Suggestion : 7

Add suffix/prefix to column names of DataFrame,In the above DataFrame, we want to add a suffix or prefix to the column names. We can use the below methods in order to do that.,A DataFrame is a two-dimensional data structure that contains columns and rows. The add_suffix() function is used to add a suffix to all column names in a DataFrame. This function is useful when we want to add a specific character to all column names in a DataFrame, such as an underscore "_".,DataFrame.where() function in Pandas

First, let's create a DataFrame

import pandas as pd

# create a dataframe
df = pd.DataFrame({
   'name': ['Rick', 'Carol', 'Daryl', 'Negan'],
   'place': ['Alexendria', 'Kingdom', 'Hilltop', 'Saviours'],
   'episode': [50, 30, 35, 29]
})

print(df)
╒════╤════════╤════════════╤═══════════╕││
name│ place│ episode│╞════╪════════╪════════════╪═══════════╡│ 0│ Rick│ Alexendria│ 50│├────┼────────┼────────────┼───────────┤│ 1│ Carol│ Kingdom│ 30│├────┼────────┼────────────┼───────────┤│ 2│ Daryl│ Hilltop│ 35│├────┼────────┼────────────┼───────────┤│ 3│ Negan│ Saviours│ 29│╘════╧════════╧════════════╧═══════════╛

Code example

import pandas as pd

# create a dataframe
df = pd.DataFrame({
   'name': ['Rick', 'Carol', 'Daryl', 'Negan'],
   'place': ['Alexendria', 'Kingdom', 'Hilltop', 'Saviours'],
   'episode': [50, 30, 35, 29]
})

# add suffix to each column
df = df.add_suffix('_watch')

print(df)

Syntax

DataFrame.add_prefix('prefix_name')
import pandas as pd

# create a dataframe
df = pd.DataFrame({
   'name': ['Rick', 'Carol', 'Daryl', 'Negan'],
   'place': ['Alexendria', 'Kingdom', 'Hilltop', 'Saviours'],
   'episode': [50, 30, 35, 29]
})

# add suffix
df = df.add_suffix('_watch')
print(df)