Reputation: 375
I have a data frame of 50 columns in it, and I want to rename each column to include the index position.
so columnA would be columnA_0, columnB would be columnB_1, and so on.
I could do manually for each line, like:
df = df.rename(columns={df.columns[1]: str(df.columns[1]) + '_'+ str(dftest.index[1])})
But I want to know if there is an easier way.
Upvotes: 1
Views: 44
Reputation: 1196
you can set the names directly:
df.columns= [f"{col}_{ix}" for ix, col in enumerate(df.columns)]
Upvotes: 4