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?
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.
You can change the column name of pandas DataFrame by using DataFrame. rename() method and DataFrame. columns() method.
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.
I should have used the re
package:
raw_data = raw_data.rename(columns=lambda x: re.sub(' $','',x))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With