ComeçarComece de graça

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.

Este exercício faz parte do curso

Intermediate Python for Finance

Ver curso

Instruções do exercício

  • 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.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

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)
Editar e executar o código