Add Suffix to Column Labels of DataFrame
To add a string after each column label of DataFrame in Pandas, call add_suffix() method on this DataFrame, and pass the suffix string as argument to add_suffix() method.
In this tutorial, we will learn how to add a string as suffix to column labels of DataFrame, with examples.
Syntax
The syntax of pandas DataFrame.add_suffix() method is
DataFrame.add_suffix(suffix)
where
Parameter | Value | Description |
---|---|---|
suffix | str | The string to add after each label. |
Return Value
DataFrame with updated labels.
Examples
In the following program, we will take a DataFrame with column labels [‘1’, ‘2’]. We add suffix string ‘_col’ to the column labels of this DataFrame using DataFrame.add_suffix() method.
Example.py
import pandas as pd
df = pd.DataFrame(
{'1': [10, 20, 30], '2': [40, 50, 60]})
suffix = '_col'
result = df.add_suffix(suffix)
print(result)
Output
1_col 2_col
0 10 40
1 20 50
2 30 60
Now, let us try with the suffix string ‘_xy’.
Example.py
import pandas as pd
df = pd.DataFrame(
{'1': [10, 20, 30], '2': [40, 50, 60]})
suffix = '_xy'
result = df.add_suffix(suffix)
print(result)
Output
1_xy 2_xy
0 10 40
1 20 50
2 30 60
Conclusion
In this Pandas Tutorial, we learned how to add a string as suffix to column labels of DataFrame, using pandas DataFrame.add_suffix() method.