0

So i have Dataframe that has around 40 columns. They contain (made up) scores for a test. The columns are now named as follows:
Student, Date, Score, Score.1, Score.2 all the way to Score.39.
We were asked to reset the column names so they match the score (change Score to Score.1, Score.1 to Score.2, Score.2 to Score.3 and so on).

My code looks like this now:

import pandas as pd
prog = pd.read_excel('File.xlsx')
for c in prog.columns:
    prog[c].rename(columns = lambda x : 'Score_' + x)

Unfortunatly this does not give the output i want it to.
I was hoping someone could show me how to do this.
Thanks in advance

2
  • cols = df.columns.tolist() and df.columns = cols[:2] + ['Score_%i' % i for i in xrange(1, len(cols[2:])+1)]? Commented Feb 9, 2017 at 20:19
  • @JohnGalt Thanks man! that is exactly what i meant. Commented Feb 9, 2017 at 22:32

1 Answer 1

1

John Galt came up with the solution in the comments: cols = df.columns.tolist() and df.columns = cols[:2] + ['Score_%i' % i for i in xrange(1, len(cols[2:])+1)]

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.