Reputation: 3
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?
Upvotes: 0
Views: 204
Reputation: 215077
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)]]
Upvotes: 2