0

I'm aiming to replace column headers in a pandas df. Particularly, I want to change the columns ending in _Item. At the moment the start of those column headers begins with whatever string is in Group_A or Group_B. But these strings will change with each new dataset. So I'm hoping to replace the appropriate string values with Group A or Group B.

At the moment, I'm manually replacing the column headers but this doesn't work if the columns aren't ordered.

df = pd.DataFrame({        
    'Group_A' : ['Red','Red','Red','Red','Red','Red'],                                   
    'Group_B' : ['Blue','Blue','Blue','Blue','Blue','Blue'],       
    'Blue_Item' : ['2','4','6','8','10','12',],                              
    'Red_Item' : ['1','2','3','4','5','6',],                                                          
    })

df.columns = [*df.columns[:-2], 'Group_A_Item','Group_B_Item']

intended output:

df = pd.DataFrame({        
    'Group_A' : ['Red','Red','Red','Red','Red','Red'],                                   
    'Group_B' : ['Blue','Blue','Blue','Blue','Blue','Blue'],       
    'Group_B_Item' : ['2','4','6','8','10','12',],                              
    'Group_A_Item' : ['1','2','3','4','5','6',],                                                          
    })
Chopin
  • 96
  • 1
  • 10
  • 35

1 Answers1

0

Simply use the rename method. To use it, provide a dict with key being the current name and associated key being the name you want in replacement. All columns name that does not appear in the dict is left unchanged. This gives the following code in your case :

df = pd.DataFrame({        
    'Group_A' : ['Red','Red','Red','Red','Red','Red'],                                   
    'Group_B' : ['Blue','Blue','Blue','Blue','Blue','Blue'],       
    'Blue_Item' : ['2','4','6','8','10','12',],                              
    'Red_Item' : ['1','2','3','4','5','6',],                                                          
    })

df = df.rename(columns={'Blue_Item': 'Group_B_Item', 'Red_Item': 'Group_A_Item'})

This way, renaming does not depends on column order.

Find more with the pandas' rename documentation.

As you specifically want to dynamically rename your columns (you said you want to replace appropriate strings with Group_A or Group_B), you can still write a method to dynamiccaly build the dictionnary to provide to the rename method.

AlexTorx
  • 753
  • 4
  • 9