Smasell
Smasell

Reputation: 1228

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

Upvotes: 0

Views: 1853

Answers (1)

Valdi_Bo
Valdi_Bo

Reputation: 31011

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)) ]

Upvotes: 2

Related Questions