インデックスの設定と解除
pandas では、列をインデックスとして指定できます。これにより、サブセットを取得する際のコードがすっきりし、場合によってはより効率的なルックアップも可能になります。
この章では、世界各地の都市の平均気温を含むDataFrameである temperatures を扱います。pandas は pd として読み込まれています。
この演習はコースの一部です
pandas によるデータ操作
演習の手順
temperaturesを確認しましょう。temperaturesのインデックスを"city"に設定し、temperatures_indに代入しましょう。temperatures_indを確認しましょう。temperaturesとどこが違いますか?temperatures_indのインデックスを、内容を保持したままリセットしましょう。temperatures_indのインデックスを、内容を削除してリセットしましょう。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Look at temperatures
print(____)
# Set the index of temperatures to city
temperatures_ind = ____
# Look at temperatures_ind
print(____)
# Reset the temperatures_ind index, keeping its contents
print(____)
# Reset the temperatures_ind index, dropping its contents
print(____)