0

I have a dataframe with 162 columns, and I want to rename the columns to variables x1, x2, ... , x(n), but keep the current column names in a dictionary for reference. I've tried a few loop methods but am not having much luck - I want to avoid doing this manually considering the number of columns. Any input is much appreciated~! Thank you.

Ex: from column names "red", "green", "blue", ... (162 total colors = column names), create dictionary {'x1':'red', 'x2':'blue,...,'xn':'lastcolor'}

aynber
  • 22,380
  • 8
  • 50
  • 63
AHN
  • 1

2 Answers2

0

Should be as simple as

columns = ['red', 'green', 'blue']
d = {f'x{i}': c for i, c in enumerate(columns, 1)}
Eugene Pakhomov
  • 9,309
  • 3
  • 27
  • 53
0

To automate this, you need to get the column names from the dataframe itself. But the DataFrame behaves like a dictionary, so iterating over it gives you back its keys (related SO question with more details here). Building on Eugene's answer, you should be able to do

new_to_old = {f'x{i}': old_name for i, old_name in enumerate(columns, df)}

where df is your dataframe. If you want to be more explicit, you could do

new_to_old = {f'x{i}': old_name for i, old_name in enumerate(columns, df.keys())}

or

new_to_old = {f'x{i}': old_name for i, old_name in enumerate(columns, df.columns.values)}

Which you choose is mostly personal preference, unless you care about performance, in which case you should use df.columns.values.

To rename the columns themselves, you could use DataFrame.rename (docs) with the inverse of new_to_old:

old_to_new = {old: new for new, old in in new_to_old.items()}

df.rename(columns = old_to_new)
Josh Karpel
  • 2,110
  • 2
  • 10
  • 21