Home > OS >  Replacing Pandas DF columns by searching through a list
Replacing Pandas DF columns by searching through a list

Time:02-25

I am working on a problem where I have a dataframe which contains data along with the columns names which are codes for some specific purpose as below:

AB_01 AB_12 AB_32 AB_50
John 26 E1 US
Tina 30 E2 CA
Michael 50 E1 UK
Shaw 55 E3 US

I have a list of list which contains column names along with their corresponding column codes as below:

[
  ['AB_01', 'Name']
  ['AB_12', 'Age']  
  ['AB_32', 'Unit']  
  ['AB_50', 'Country']
  ['BZ_90', 'Zip']
  ['CX_10', 'State']
  ['ED_55', 'Email']
]

Some of the columns might not be used in the dataframes so I'd like to search for the column codes from the list and then replace the column codes with the names in the list so that I have something like below:

Name Age Unit Country
John 26 E1 US
Tina 30 E2 CA
Michael 50 E1 UK
Shaw 55 E3 US

What would be the best way of doing this? The one I think of is iterating through each column code in the dataframe, comparing it with the column code in the list and then replacing it with the corresponding column name from the list. Is there a better way of doing it?

CodePudding user response:

You can convert the list to a dictionary and map it to the columns:

df.columns = df.columns.map(dict(lst))

Output:

      Name  Age Unit Country
0     John   26   E1      US
1     Tina   30   E2      CA
2  Michael   50   E1      UK
3     Shaw   55   E3      US

CodePudding user response:

Let us try rename ,l is your list

out = df.rename(columns = dict(l))
Out[332]: 
      Name  Age Unit Country
0     John   26   E1      US
1     Tina   30   E2      CA
2  Michael   50   E1      UK
3     Shaw   55   E3      US
  • Related