Reputation: 123
I want to change the names of every column of my dataframe iterating over each column names
I am able to change the column names one by one but i want to use a for loop in order to change all column names
for i in range(0,len(flattened.columns)):
flattened.rename(columns={flattened.columns[i]: "P" + str(i)})
Upvotes: 0
Views: 1739
Reputation: 306
You could just create the dictionary for rename
in a list comprehension and then apply it to all columns in a single step, like so:
flattened.rename(
columns = {
column_name: 'P' + str(index) for index,column_name in enumerate(flattened.columns)
}
)
Is this what you are looking for?
Upvotes: 1