Add Suffix to Rows and Columns in Pandas

Spread the love

In this post you will learn how to add suffix to rows and columns in pandas. To add suffix in pandas we use the add_suffix() method. When applies to series, the suffix gets added to the rows and when added to a dataframe it gets added to the columns of the dataframe.

Syntax –

DataFrame.add_suffix(suffix)

suffix – The string to add after each label.

Examples –

1 . Add Suffix to Rows or Index –

When we apply a suffix 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 suffix to each row labels

s.add_suffix('_item')

2 . Add Suffix to Columns –

When we applies a suffix 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 suffix to each columns of the DataFrame.

df.add_suffix('_col')

Rating: 1 out of 5.

Leave a Reply