1

edStatsData is the dataset I am manipulating

edStatsData is the dataset I am manipulating, I want to display only the two first columns that you see in line 85 plus the columns from year 1995 to 2015 that is shown in 81, plus the columns from 2025 to 2050, how to create this data frame with only the columns that I need ? ( with python , pandas)

details about type

Sofia
  • 29
  • 5

2 Answers2

3

Use:

# if years are int
cols = ['Country Code', 'Country Name'] \
       + list(range(1995, 2016)) \
       + list(range(2025, 2051))

# OR

# if years are str
cols = ['Country Code', 'Country Name'] \
       + [str(y) for y in range(1995, 2016)] \
       + [str(y) for y in range(2025, 2051)]

# Select subset of columns
print(df[cols])
mozway
  • 194,879
  • 13
  • 39
  • 75
Corralien
  • 109,409
  • 8
  • 28
  • 52
0

the best way to create such a data-frame would be creating separate data-frames as you have defined and then appending those side-by-side.

step1: create data-frames with your desired columns
extracting specified selected columns to a new dataframe

step2: join the data-frames side-by-side
combine two data-frames in python

hansrajswapnil
  • 549
  • 1
  • 6
  • 14