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)
CodePudding user response:
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
CodePudding user response:
Use:
cols = ['Country Code', 'Country Name'] \
[y for y in range(1995, 2016)] \
[y for y in range(2025, 2051)]
# OR
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])