
In this post you will learn how to add prefix to rows and columns in pandas. To add prefix in pandas we use the add_prefix() method. When applies to series, the prefix gets added to the rows and when added to a dataframe it gets added to the columns of the dataframe.
Syntax –
DataFrame.add_prefix(prefix)
prefix – The string to add before each label.
Examples –
1 . Add Prefix to rows or Index –
When we apply a prefix to a series, pandas applies it to the row labels.
import pandas as pd
s = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
s

Let’s add a prefix to each row labels
s.add_prefix('item_')

2 . Add Prefix to Columns –
When we applies a prefix to a DataFrame, Pandas applies it to the columns of the DataFrame.
Let’s create a DataFrame.
df = pd.DataFrame({'A':[1, 2, 3, 4, 5],
'B':[6, 7, 8, 9, 10],
'C':[11, 12, 13, 14, 15]})
df

Now, Let’s add a prefix to each columns of the DataFrame.
df.add_prefix('col_')
