Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

renaming column in dataframe for Pandas using regular expression

Tags:

I have a dataframe made by Pandas that I want to remove the empty space at the end of each column name. I tried something like:

raw_data.columns.values = re.sub(' $','',raw_data.columns.values) 

But this is not working, anything I did wrong here?

like image 507
lokheart Avatar asked Oct 22 '14 04:10

lokheart


People also ask

How do I rename a column in a DataFrame list?

Rename Columns with List using set_axis() Alternatively, you can use DataFrame. set_axis() method to rename columns with list. use inplace=True param to rename columns on the existing DataFrame object.

How do I change the column name in one column pandas?

You can change the column name of pandas DataFrame by using DataFrame. rename() method and DataFrame. columns() method.

How do I rename a column name in pandas CSV?

Using rename() function Pandas has a built-in function called rename() to change the column names. It's useful when you want to rename some selected columns.


1 Answers

I should have used the re package:

raw_data = raw_data.rename(columns=lambda x: re.sub(' $','',x)) 
like image 58
lokheart Avatar answered Sep 19 '22 19:09

lokheart