-1

I want to get the 'Country Name' columns and also the years from 2006 to 2015.

This is what the df looks like:

GPD dataframe

I've tried the following, but it didn't work.

GPD.loc[:,'Country Name',['2006':'2015']]
mozway
  • 194,879
  • 13
  • 39
  • 75
  • 3
    [Please do not upload images of code/data/errors](//meta.stackoverflow.com/q/285551). [pandas](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples), [reprex](https://stackoverflow.com/help/minimal-reproducible-example) – J_H Mar 26 '23 at 20:56

1 Answers1

3

You can use range and unpacking:

GPD.loc[:, ['Country Name', *range(2006, 2015+1)]]

Or if the years are strings:

GPD.loc[:, ['Country Name', *map(str, range(2006, 2015+1))]]

Or a double slicing:

GPD.loc[:, ['Country Name']+list(GPD.loc[:, '2006':'2015'])]
mozway
  • 194,879
  • 13
  • 39
  • 75