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.
Diese Übung ist Teil des Kurses
Intermediate Python for Finance
Anleitung zur Übung
- Print the columns of the
pceDataFrame. - Create a new DataFrame from
pcewithout the columns in the listcolumns_to_drop. - Print the columns of the new DataFrame.
- Drop the columns in
columns_to_dropfrom the DataFramepcein place.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
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)