Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming column names in Pandas

I have a DataFrame using Pandas and column labels that I need to edit to replace the original column labels.

I'd like to change the column names in a DataFrame A where the original column names are:

['$a', '$b', '$c', '$d', '$e'] 

to

['a', 'b', 'c', 'd', 'e']. 

I have the edited column names stored it in a list, but I don't know how to replace the column names.

like image 266
user1504276 Avatar asked Jul 05 '12 14:07

user1504276


People also ask

How do I rename column names?

To change a column name, enter the following statement in your MySQL shell: ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name; Replace table_name , old_column_name , and new_column_name with your table and column names.

How do I rename a column in Python?

You can rename the columns using the rename() method by using the axis keyword in it. In this method, you'll specify the columns as Python Set within { } rather specifying columns as a Python Dictionary with Key-Value Pairs.


2 Answers

RENAME SPECIFIC COLUMNS

Use the df.rename() function and refer the columns to be renamed. Not all the columns have to be renamed:

df = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}) # Or rename the existing DataFrame (rather than creating a copy)  df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}, inplace=True) 

Minimal Code Example

df = pd.DataFrame('x', index=range(3), columns=list('abcde')) df     a  b  c  d  e 0  x  x  x  x  x 1  x  x  x  x  x 2  x  x  x  x  x 

The following methods all work and produce the same output:

df2 = df.rename({'a': 'X', 'b': 'Y'}, axis=1)  # new method df2 = df.rename({'a': 'X', 'b': 'Y'}, axis='columns') df2 = df.rename(columns={'a': 'X', 'b': 'Y'})  # old method    df2     X  Y  c  d  e 0  x  x  x  x  x 1  x  x  x  x  x 2  x  x  x  x  x 

Remember to assign the result back, as the modification is not-inplace. Alternatively, specify inplace=True:

df.rename({'a': 'X', 'b': 'Y'}, axis=1, inplace=True) df     X  Y  c  d  e 0  x  x  x  x  x 1  x  x  x  x  x 2  x  x  x  x  x   

From v0.25, you can also specify errors='raise' to raise errors if an invalid column-to-rename is specified. See v0.25 rename() docs.


REASSIGN COLUMN HEADERS

Use df.set_axis() with axis=1 and inplace=False (to return a copy).

df2 = df.set_axis(['V', 'W', 'X', 'Y', 'Z'], axis=1, inplace=False) df2     V  W  X  Y  Z 0  x  x  x  x  x 1  x  x  x  x  x 2  x  x  x  x  x 

This returns a copy, but you can modify the DataFrame in-place by setting inplace=True (this is the default behaviour for versions <=0.24 but is likely to change in the future).

You can also assign headers directly:

df.columns = ['V', 'W', 'X', 'Y', 'Z'] df     V  W  X  Y  Z 0  x  x  x  x  x 1  x  x  x  x  x 2  x  x  x  x  x 
like image 160
lexual Avatar answered Oct 06 '22 00:10

lexual


Just assign it to the .columns attribute:

>>> df = pd.DataFrame({'$a':[1,2], '$b': [10,20]}) >>> df    $a  $b 0   1  10 1   2  20  >>> df.columns = ['a', 'b'] >>> df    a   b 0  1  10 1  2  20 
like image 31
eumiro Avatar answered Oct 05 '22 23:10

eumiro