DataFrame から列を削除する
ときどき、DataFrame にもう不要な列が含まれていることがあります。列を削除して DataFrame をシンプルにする必要があることはよくあります。行や列を削除するのに使うメソッドを、動画で見たのを覚えていますか?
会社の関係者に PCE の計算結果を提示するとして、計算に使った列は見せたくないとします。提供された DataFrame pce から、PCDG、PCND、PCESV の列を削除し、PCE 列だけを残してください。columns_to_drop のリストが用意されています。
この演習はコースの一部です
Intermediate Python for Finance
演習の手順
pceDataFrame の列名を表示してください。- リスト
columns_to_dropに含まれる列を除いて、pceから新しい DataFrame を作成してください。 - 新しい DataFrame の列名を表示してください。
pceDataFrame からcolumns_to_dropの列をその場で(in place)削除してください。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
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)