ワイド形式からロング形式へのアンピボット
Spotify データに対して groupby 集約を行い、アーティストごとの年間ストリーム数について、さまざまな統計量を含む DataFrame wide_df を作成しました。これをデータ可視化で使うため、wide_df のデータをロング形式に変換したいとします。
この演習はコースの一部です
Polars入門
演習の手順
- 各行のデータを識別するため、
"artist"と"year"をインデックス列として使います。 wide_dfの統計列"avg_streams"、"max_streams"、"min_streams"をアンピボットします。- 変数列名を
"metric"、値の列名を"stream_count"にします。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
long_df = (
wide_df
.unpivot(
# Set the index columns
index=____,
# Unpivot on the stats columns
on=____,
# Name the variable and value columns
variable_name=____,
value_name=____
)
)
print("\nLong format DataFrame:")
print(long_df)