始める無料で始める

カテゴリ変数のカウント

カウントはデータの全体像を把握し、見落としがちな特徴に気づくための有効な手法です。この演習では、前の演習で作成したDataFrameを使って、店舗の種類ごとの店舗数と、部門番号ごとの数をカウントします。

# Drop duplicate store/type combinations
store_types = sales.drop_duplicates(subset=["store", "type"])

# Drop duplicate store/department combinations
store_depts = sales.drop_duplicates(subset=["store", "department"])

前の演習で作成した store_typesstore_depts のDataFrameはすでに用意されており、pandaspd としてインポートされています。

この演習はコースの一部です

pandas によるデータ操作

コースを見る

演習の手順

  • store_types で、type ごとの店舗数をカウントしましょう。
  • store_types で、type ごとの店舗の割合を求めましょう。
  • store_depts で、department ごとの店舗数をカウントし、カウントを降順に並べ替えましょう。
  • store_depts で、department ごとの店舗の割合を求め、割合を降順に並べ替えましょう。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

# Count the number of stores of each type
store_counts = ____
print(store_counts)

# Get the proportion of stores of each type
store_props = ____
print(store_props)

# Count the number of stores for each department and sort
dept_counts_sorted = ____
print(dept_counts_sorted)

# Get the proportion of stores in each department and sort
dept_props_sorted = ____.____(sort=____, normalize=____)
print(dept_props_sorted)
コードを編集して実行