0

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

3
  • why did you not just do this? : df = pd.DataFrame(data, columns = ['param1','param2','param3','param4']) Commented Jun 6, 2021 at 16:08
  • @Theshape because a lot of columns Commented Jun 6, 2021 at 16:14
  • o i see @Smasell Commented Jun 6, 2021 at 16:16

1 Answer 1

2

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)) ]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.