0

I have a Dataframe with a Country column as well as year columns (spanning from 1960 - 2015). I wish to create a new Dataframe only containing the Country and the columns within the last ten years.

I have managed to get the last ten years into a Dataframe but not the Countries they correspond to.

df = df[['Country', '%d' % i for i in range(2006, 2016)]]

From the code above, I get returned a syntax error. What am I doing wrong?

1 Answer 1

2

The for syntax in python needs to be wrapped either in parenthesis (creating a generator) or in brackets (creating a list). You need a list of columns in this case. So wrap the for loop in a pair of bracket to make it a list of columns and then concatenate it with Country with +:

df = df[['Country'] + ['%d' % i for i in range(2006, 2016)]]

Or as @DeepSpace commented, use str method to convert integer to string:

df = df[['Country'] + [str(i) for i in range(2006, 2016)]]
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, but '%d' % i feels really unnecessary here. str(i) will be enough.
Thanks for the help and suggestion.

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.