+3 votes
in Programming Languages by (34.9k points)
I want to add a prefix to all column names of a pandas dataframe. Is there any function for it?

1 Answer

+2 votes
by (51.1k points)
edited by

You can use the add_prefix() function of Pandas DataFrame. This function adds a prefix to labels for Series and columns for DataFrame.

Here is an example:

>>> import pandas as pd

>>> df = pd.DataFrame({'A':[1,2,3],'B':[11,12,13]})

>>> df

   A   B

0  1  11

1  2  12

2  3  13

>>> df.add_prefix('col_')

   col_A  col_B

0      1     11

1      2     12

2      3     13


...