Reputation: 513
I have 5 dataframes called control1,control2,control3,control4,control5. I'm renaming their columns. But i want to rename the column named 10 (integer name) with the elements in files for each one. Something like this:
control1.rename(columns={10:'02042020.txt'}, inplace=True)
control2.rename(columns={10:'05032020.txt'}, inplace=True)
#...... etc etc
This is my code:
lista_controles=[control1,control2,control3,control4,control5]
files=['02042020.txt','05032020.txt','12032020.txt','19032020.txt','26032020.txt']
for df in lista_controles:
df.rename(columns={2:'NOMBRE_ESTACION'}, inplace=True)
for i in files
df.rename(columns={10:f'{i}'}, inplace=True)
But is only renaming with the first element of the list (02042020.txt). How can i solve this? Thanks..
Upvotes: 0
Views: 67
Reputation: 1765
This is because when df.rename(columns={10:f'{i}'}, inplace=True)
first runs, it changes the name "10" to "02042020.txt". At the second iteration, there is no any column named "10" anymore, so it does nothing, because it just has been changed to "02042020.txt".
You could try:
for i, df in enumerate(lista_controles):
df.rename(columns={2:'NOMBRE_ESTACION'}, inplace=True)
df.rename(columns={10:files[i]}, inplace=True)
To make this work, make sure you have equal number of elements in lista_controles
and files
though. If there are not, there are other ways to do it.
Upvotes: 1