how to replace characters in multiple column name in pandas

276 views
Skip to first unread message

Steven

unread,
Jun 8, 2018, 9:29:47 PM6/8/18
to PyData
I'm trying to find a way to replace characters in multiple column names. I want to change "hld" to "pct" in the name from my 2th and 6th columns. I tried the following code:

df_hld.columns.values[2:7] = df_hld.columns[2:7].str.replace("hld", "pct")

It works in terms of changing names, but later on when I tried to merge the data frame with another one. All the values in those columns returned NaN. I went back to check the dtypes, and they are indeed float64. I really don't know what went wrong here.

Please help! 

Peter Leimbigler

unread,
Jun 9, 2018, 3:35:38 PM6/9/18
to PyData
Pandas columns are Index objects, which are immutable, meaning you can't change individual column names in an existing DataFrame. But you can reassign the Index object that df.columns points to, like this:

df_hld.columns = df_hld.columns.str.replace("hld", "pct")

A similar question with helpful answers: https://stackoverflow.com/q/38299205

Steven

unread,
Jun 9, 2018, 4:33:30 PM6/9/18
to PyData
but this will replace the characters in all of my columns names. I'm confused that why I can't use .values method. 

Peter Leimbigler

unread,
Jun 9, 2018, 5:27:41 PM6/9/18
to PyData
Apologies, I misread your question. This is the only concise way I can think of to replace strings in the names of columns 2 through 6:

df_hld.rename(columns={c: c.replace('hld', 'pct') for c in df_hld.columns[2:7]}, inplace=True)

Steven

unread,
Jun 9, 2018, 5:36:54 PM6/9/18
to PyData
Thank you so much buddy!!!!!! It works great!
Reply all
Reply to author
Forward
0 new messages