Get startedGet started for free

Dropping columns from DataFrame

Sometimes your DataFrame has columns that you no longer need. It is often the case that you need to simplify a DataFrame by removing columns. Do you remember from the video which method you need to use to drop a row or a column?

Pretend that you wish to present the results of your PCE calculation to stakeholders at your company, without presenting the columns you used for the calculation. Drop the columns PCDG, PCND, and PCESV from the supplied DataFrame pce, leaving only the column PCE. The list columns_to_drop is provided.

This exercise is part of the course

Intermediate Python for Finance

View Course

Exercise instructions

  • Print the columns of the pce DataFrame.
  • Create a new DataFrame from pce without the columns in the list columns_to_drop.
  • Print the columns of the new DataFrame.
  • Drop the columns in columns_to_drop from the DataFrame pce in place.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

columns_to_drop = ['PCDG', 'PCND', 'PCESV']

# Print the current columns of the DataFrame pce
____(pce.columns)

# Create new_pce by dropping columns_to_drop from pce
new_pce = pce.drop(columns=____)
# Print the columns of the new DataFrame
print(new_pce.____)

# Drop the columns in_place in the original DataFrame
pce.____(columns=columns_to_drop, inplace=____)

# Print the columns of the DataFrame pce
print(pce.columns)
Edit and Run Code