Rename column names through the loop (Python)



I have a table:

I have the table like this:

import pandas as pd
data = [[20, 15, 10, 5], [20, 15, 10, 5], [20, 15, 10, 5], [20, 15, 10, 5]]
df = pd.DataFrame(data, columns = ['asd', 'bsd', 'tsd', 'pzd'])
df
asd bsd tsd pzd
20 15 10 5
20 15 10 5
20 15 10 5
20 15 10 5

I want to rename all my column names with the pattern like this 'param'+ (index_column +1) through the loop

Desired output:

param1 param2 param3 param4
20 15 10 5
20 15 10 5
20 15 10 5
20 15 10 5

Thanks

Answer

No need to use any loop. Just create a new list of column names, in a list comprehension and set it as new column names:

df.columns = [ 'param' + str(i + 1) for i in range(len(df.columns)) ]


Source: stackoverflow