{
"cells": [
{
"cell_type": "markdown",
"id": "41fb9eda-8d3e-43e6-ac29-ed5bb545f0e3",
"metadata": {},
"source": [
"# データサイエンス100本ノック 11~20"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "bfa0c462-fa3a-443e-b011-1094b8e3d629",
"metadata": {},
"outputs": [],
"source": [
"import polars as pl\n",
"from helper.polars import load_100knocks_data\n",
"pl.Config.set_fmt_str_lengths(100)\n",
"df_customer, df_category, df_product, df_receipt, df_store, df_geocode = load_100knocks_data()"
]
},
{
"cell_type": "markdown",
"id": "59d6a9f8-31bf-40fe-99c2-a51866f92731",
"metadata": {},
"source": [
"## P-011\n",
"\n",
"顧客データ(df_customer)から顧客ID(customer_id)の末尾が1のものだけ全項目抽出し、10件表示せよ。"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "8c93befd-0a1e-4705-b1d8-072e56105267",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"
shape: (5, 11)customer_id | customer_name | gender_cd | gender | birth_day | age | postal_cd | address | application_store_cd | application_date | status_cd |
---|
str | str | str | str | str | i64 | str | str | str | i64 | str |
"CS037613000071" | "六角 雅彦" | "9" | "不明" | "1952-04-01" | 66 | "136-0076" | "東京都江東区南砂**********" | "S13037" | 20150414 | "0-00000000-0" |
"CS028811000001" | "堀井 かおり" | "1" | "女性" | "1933-03-27" | 86 | "245-0016" | "神奈川県横浜市泉区和泉町**********" | "S14028" | 20160115 | "0-00000000-0" |
"CS040412000191" | "川井 郁恵" | "1" | "女性" | "1977-01-05" | 42 | "226-0021" | "神奈川県横浜市緑区北八朔町**********" | "S14040" | 20151101 | "1-20091025-4" |
"CS028314000011" | "小菅 あおい" | "1" | "女性" | "1983-11-26" | 35 | "246-0038" | "神奈川県横浜市瀬谷区宮沢**********" | "S14028" | 20151123 | "1-20080426-5" |
"CS039212000051" | "藤島 恵梨香" | "1" | "女性" | "1997-02-03" | 22 | "166-0001" | "東京都杉並区阿佐谷北**********" | "S13039" | 20171121 | "1-20100215-4" |
"
],
"text/plain": [
"shape: (5, 11)\n",
"┌───────────────┬───────────────┬───────────┬────────┬───┬───────────────┬───────────────┬──────────────┬──────────────┐\n",
"│ customer_id ┆ customer_name ┆ gender_cd ┆ gender ┆ … ┆ address ┆ application_s ┆ application_ ┆ status_cd │\n",
"│ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ tore_cd ┆ date ┆ --- │\n",
"│ str ┆ str ┆ str ┆ str ┆ ┆ str ┆ --- ┆ --- ┆ str │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ str ┆ i64 ┆ │\n",
"╞═══════════════╪═══════════════╪═══════════╪════════╪═══╪═══════════════╪═══════════════╪══════════════╪══════════════╡\n",
"│ CS03761300007 ┆ 六角 雅彦 ┆ 9 ┆ 不明 ┆ … ┆ 東京都江東区 ┆ S13037 ┆ 20150414 ┆ 0-00000000-0 │\n",
"│ 1 ┆ ┆ ┆ ┆ ┆ 南砂********* ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ * ┆ ┆ ┆ │\n",
"│ CS02881100000 ┆ 堀井 かおり ┆ 1 ┆ 女性 ┆ … ┆ 神奈川県横浜 ┆ S14028 ┆ 20160115 ┆ 0-00000000-0 │\n",
"│ 1 ┆ ┆ ┆ ┆ ┆ 市泉区和泉町* ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ ********* ┆ ┆ ┆ │\n",
"│ CS04041200019 ┆ 川井 郁恵 ┆ 1 ┆ 女性 ┆ … ┆ 神奈川県横浜 ┆ S14040 ┆ 20151101 ┆ 1-20091025-4 │\n",
"│ 1 ┆ ┆ ┆ ┆ ┆ 市緑区北八朔 ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ 町********** ┆ ┆ ┆ │\n",
"│ CS02831400001 ┆ 小菅 あおい ┆ 1 ┆ 女性 ┆ … ┆ 神奈川県横浜 ┆ S14028 ┆ 20151123 ┆ 1-20080426-5 │\n",
"│ 1 ┆ ┆ ┆ ┆ ┆ 市瀬谷区宮沢* ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ ********* ┆ ┆ ┆ │\n",
"│ CS03921200005 ┆ 藤島 恵梨香 ┆ 1 ┆ 女性 ┆ … ┆ 東京都杉並区 ┆ S13039 ┆ 20171121 ┆ 1-20100215-4 │\n",
"│ 1 ┆ ┆ ┆ ┆ ┆ 阿佐谷北***** ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ ***** ┆ ┆ ┆ │\n",
"└───────────────┴───────────────┴───────────┴────────┴───┴───────────────┴───────────────┴──────────────┴──────────────┘"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(\n",
"df_customer\n",
".filter(pl.col.customer_id.str.ends_with('1'))\n",
".head(5)\n",
")"
]
},
{
"cell_type": "markdown",
"id": "0c2b6484-3649-4e87-a1a0-a151d223a554",
"metadata": {},
"source": [
"## P-012\n",
"\n",
"店舗データ(df_store)から、住所 (address) に\"横浜市\"が含まれるものだけ全項目表示せよ。"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f1748621-78e0-4097-ade4-2b10c13adad2",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
shape: (11, 10)store_cd | store_name | prefecture_cd | prefecture | address | address_kana | tel_no | longitude | latitude | floor_area |
---|
str | str | str | str | str | str | str | f64 | f64 | f64 |
"S14010" | "菊名店" | "14" | "神奈川県" | "神奈川県横浜市港北区菊名一丁目" | "カナガワケンヨコハマシコウホククキクナイッチョウメ" | "045-123-4032" | 139.6326 | 35.50049 | 1732.0 |
"S14033" | "阿久和店" | "14" | "神奈川県" | "神奈川県横浜市瀬谷区阿久和西一丁目" | "カナガワケンヨコハマシセヤクアクワニシイッチョウメ" | "045-123-4043" | 139.4961 | 35.45918 | 1495.0 |
"S14040" | "長津田店" | "14" | "神奈川県" | "神奈川県横浜市緑区長津田みなみ台五丁目" | "カナガワケンヨコハマシミドリクナガツタミナミダイゴチョウメ" | "045-123-4046" | 139.4994 | 35.52398 | 1548.0 |
"S14050" | "阿久和西店" | "14" | "神奈川県" | "神奈川県横浜市瀬谷区阿久和西一丁目" | "カナガワケンヨコハマシセヤクアクワニシイッチョウメ" | "045-123-4053" | 139.4961 | 35.45918 | 1830.0 |
"S14028" | "二ツ橋店" | "14" | "神奈川県" | "神奈川県横浜市瀬谷区二ツ橋町" | "カナガワケンヨコハマシセヤクフタツバシチョウ" | "045-123-4042" | 139.4963 | 35.46304 | 1574.0 |
… | … | … | … | … | … | … | … | … | … |
"S14046" | "北山田店" | "14" | "神奈川県" | "神奈川県横浜市都筑区北山田一丁目" | "カナガワケンヨコハマシツヅキクキタヤマタイッチョウメ" | "045-123-4049" | 139.5916 | 35.56189 | 831.0 |
"S14011" | "日吉本町店" | "14" | "神奈川県" | "神奈川県横浜市港北区日吉本町四丁目" | "カナガワケンヨコハマシコウホククヒヨシホンチョウヨンチョウメ" | "045-123-4033" | 139.6316 | 35.54655 | 890.0 |
"S14048" | "中川中央店" | "14" | "神奈川県" | "神奈川県横浜市都筑区中川中央二丁目" | "カナガワケンヨコハマシツヅキクナカガワチュウオウニチョウメ" | "045-123-4051" | 139.5758 | 35.54912 | 1657.0 |
"S14042" | "新山下店" | "14" | "神奈川県" | "神奈川県横浜市中区新山下二丁目" | "カナガワケンヨコハマシナカクシンヤマシタニチョウメ" | "045-123-4047" | 139.6593 | 35.43894 | 1044.0 |
"S14006" | "葛が谷店" | "14" | "神奈川県" | "神奈川県横浜市都筑区葛が谷" | "カナガワケンヨコハマシツヅキククズガヤ" | "045-123-4031" | 139.5633 | 35.53573 | 1886.0 |
"
],
"text/plain": [
"shape: (11, 10)\n",
"┌──────────┬────────────┬───────────────┬────────────┬───┬──────────────┬───────────┬──────────┬────────────┐\n",
"│ store_cd ┆ store_name ┆ prefecture_cd ┆ prefecture ┆ … ┆ tel_no ┆ longitude ┆ latitude ┆ floor_area │\n",
"│ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- │\n",
"│ str ┆ str ┆ str ┆ str ┆ ┆ str ┆ f64 ┆ f64 ┆ f64 │\n",
"╞══════════╪════════════╪═══════════════╪════════════╪═══╪══════════════╪═══════════╪══════════╪════════════╡\n",
"│ S14010 ┆ 菊名店 ┆ 14 ┆ 神奈川県 ┆ … ┆ 045-123-4032 ┆ 139.6326 ┆ 35.50049 ┆ 1732.0 │\n",
"│ S14033 ┆ 阿久和店 ┆ 14 ┆ 神奈川県 ┆ … ┆ 045-123-4043 ┆ 139.4961 ┆ 35.45918 ┆ 1495.0 │\n",
"│ S14040 ┆ 長津田店 ┆ 14 ┆ 神奈川県 ┆ … ┆ 045-123-4046 ┆ 139.4994 ┆ 35.52398 ┆ 1548.0 │\n",
"│ S14050 ┆ 阿久和西店 ┆ 14 ┆ 神奈川県 ┆ … ┆ 045-123-4053 ┆ 139.4961 ┆ 35.45918 ┆ 1830.0 │\n",
"│ S14028 ┆ 二ツ橋店 ┆ 14 ┆ 神奈川県 ┆ … ┆ 045-123-4042 ┆ 139.4963 ┆ 35.46304 ┆ 1574.0 │\n",
"│ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … │\n",
"│ S14046 ┆ 北山田店 ┆ 14 ┆ 神奈川県 ┆ … ┆ 045-123-4049 ┆ 139.5916 ┆ 35.56189 ┆ 831.0 │\n",
"│ S14011 ┆ 日吉本町店 ┆ 14 ┆ 神奈川県 ┆ … ┆ 045-123-4033 ┆ 139.6316 ┆ 35.54655 ┆ 890.0 │\n",
"│ S14048 ┆ 中川中央店 ┆ 14 ┆ 神奈川県 ┆ … ┆ 045-123-4051 ┆ 139.5758 ┆ 35.54912 ┆ 1657.0 │\n",
"│ S14042 ┆ 新山下店 ┆ 14 ┆ 神奈川県 ┆ … ┆ 045-123-4047 ┆ 139.6593 ┆ 35.43894 ┆ 1044.0 │\n",
"│ S14006 ┆ 葛が谷店 ┆ 14 ┆ 神奈川県 ┆ … ┆ 045-123-4031 ┆ 139.5633 ┆ 35.53573 ┆ 1886.0 │\n",
"└──────────┴────────────┴───────────────┴────────────┴───┴──────────────┴───────────┴──────────┴────────────┘"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(\n",
"df_store\n",
".filter(pl.col.address.str.contains(\"横浜市\"))\n",
")"
]
},
{
"cell_type": "markdown",
"id": "3120516a-22de-46c6-b9d3-867b05155e38",
"metadata": {},
"source": [
"## P-013\n",
"\n",
"顧客データ(df_customer)から、ステータスコード(status_cd)の先頭がアルファベットのA〜Fで始まるデータを全項目抽出し、10件表示せよ。"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "54631ca2-a602-46a1-b0a4-bc9761b24115",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
shape: (5, 11)customer_id | customer_name | gender_cd | gender | birth_day | age | postal_cd | address | application_store_cd | application_date | status_cd |
---|
str | str | str | str | str | i64 | str | str | str | i64 | str |
"CS031415000172" | "宇多田 貴美子" | "1" | "女性" | "1976-10-04" | 42 | "151-0053" | "東京都渋谷区代々木**********" | "S13031" | 20150529 | "D-20100325-C" |
"CS015414000103" | "奥野 陽子" | "1" | "女性" | "1977-08-09" | 41 | "136-0073" | "東京都江東区北砂**********" | "S13015" | 20150722 | "B-20100609-B" |
"CS011215000048" | "芦田 沙耶" | "1" | "女性" | "1992-02-01" | 27 | "223-0062" | "神奈川県横浜市港北区日吉本町**********" | "S14011" | 20150228 | "C-20100421-9" |
"CS029415000023" | "梅田 里穂" | "1" | "女性" | "1976-01-17" | 43 | "279-0043" | "千葉県浦安市富士見**********" | "S12029" | 20150610 | "D-20100918-E" |
"CS035415000029" | "寺沢 真希" | "9" | "不明" | "1977-09-27" | 41 | "158-0096" | "東京都世田谷区玉川台**********" | "S13035" | 20141220 | "F-20101029-F" |
"
],
"text/plain": [
"shape: (5, 11)\n",
"┌───────────────┬───────────────┬───────────┬────────┬───┬───────────────┬───────────────┬──────────────┬──────────────┐\n",
"│ customer_id ┆ customer_name ┆ gender_cd ┆ gender ┆ … ┆ address ┆ application_s ┆ application_ ┆ status_cd │\n",
"│ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ tore_cd ┆ date ┆ --- │\n",
"│ str ┆ str ┆ str ┆ str ┆ ┆ str ┆ --- ┆ --- ┆ str │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ str ┆ i64 ┆ │\n",
"╞═══════════════╪═══════════════╪═══════════╪════════╪═══╪═══════════════╪═══════════════╪══════════════╪══════════════╡\n",
"│ CS03141500017 ┆ 宇多田 貴美子 ┆ 1 ┆ 女性 ┆ … ┆ 東京都渋谷区 ┆ S13031 ┆ 20150529 ┆ D-20100325-C │\n",
"│ 2 ┆ ┆ ┆ ┆ ┆ 代々木******* ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ *** ┆ ┆ ┆ │\n",
"│ CS01541400010 ┆ 奥野 陽子 ┆ 1 ┆ 女性 ┆ … ┆ 東京都江東区 ┆ S13015 ┆ 20150722 ┆ B-20100609-B │\n",
"│ 3 ┆ ┆ ┆ ┆ ┆ 北砂********* ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ * ┆ ┆ ┆ │\n",
"│ CS01121500004 ┆ 芦田 沙耶 ┆ 1 ┆ 女性 ┆ … ┆ 神奈川県横浜 ┆ S14011 ┆ 20150228 ┆ C-20100421-9 │\n",
"│ 8 ┆ ┆ ┆ ┆ ┆ 市港北区日吉 ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ 本町********* ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ * ┆ ┆ ┆ │\n",
"│ CS02941500002 ┆ 梅田 里穂 ┆ 1 ┆ 女性 ┆ … ┆ 千葉県浦安市 ┆ S12029 ┆ 20150610 ┆ D-20100918-E │\n",
"│ 3 ┆ ┆ ┆ ┆ ┆ 富士見******* ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ *** ┆ ┆ ┆ │\n",
"│ CS03541500002 ┆ 寺沢 真希 ┆ 9 ┆ 不明 ┆ … ┆ 東京都世田谷 ┆ S13035 ┆ 20141220 ┆ F-20101029-F │\n",
"│ 9 ┆ ┆ ┆ ┆ ┆ 区玉川台***** ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ ***** ┆ ┆ ┆ │\n",
"└───────────────┴───────────────┴───────────┴────────┴───┴───────────────┴───────────────┴──────────────┴──────────────┘"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(\n",
"df_customer\n",
".filter(pl.col.status_cd.str.contains('^[A-F]'))\n",
".head(5)\n",
")"
]
},
{
"cell_type": "markdown",
"id": "e5a13ce9-33c6-454b-baf6-dab4236050fd",
"metadata": {},
"source": [
"## P-014\n",
"\n",
"顧客データ(df_customer)から、ステータスコード(status_cd)の末尾が数字の1〜9で終わるデータを全項目抽出し、10件表示せよ。"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "8a23291b-42b7-4557-b490-1d1aa09b3399",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
shape: (5, 11)customer_id | customer_name | gender_cd | gender | birth_day | age | postal_cd | address | application_store_cd | application_date | status_cd |
---|
str | str | str | str | str | i64 | str | str | str | i64 | str |
"CS001215000145" | "田崎 美紀" | "1" | "女性" | "1995-03-29" | 24 | "144-0055" | "東京都大田区仲六郷**********" | "S13001" | 20170605 | "6-20090929-2" |
"CS033513000180" | "安斎 遥" | "1" | "女性" | "1962-07-11" | 56 | "241-0823" | "神奈川県横浜市旭区善部町**********" | "S14033" | 20150728 | "6-20080506-5" |
"CS011215000048" | "芦田 沙耶" | "1" | "女性" | "1992-02-01" | 27 | "223-0062" | "神奈川県横浜市港北区日吉本町**********" | "S14011" | 20150228 | "C-20100421-9" |
"CS040412000191" | "川井 郁恵" | "1" | "女性" | "1977-01-05" | 42 | "226-0021" | "神奈川県横浜市緑区北八朔町**********" | "S14040" | 20151101 | "1-20091025-4" |
"CS009315000023" | "皆川 文世" | "1" | "女性" | "1980-04-15" | 38 | "154-0012" | "東京都世田谷区駒沢**********" | "S13009" | 20150319 | "5-20080322-1" |
"
],
"text/plain": [
"shape: (5, 11)\n",
"┌───────────────┬───────────────┬───────────┬────────┬───┬───────────────┬───────────────┬──────────────┬──────────────┐\n",
"│ customer_id ┆ customer_name ┆ gender_cd ┆ gender ┆ … ┆ address ┆ application_s ┆ application_ ┆ status_cd │\n",
"│ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ tore_cd ┆ date ┆ --- │\n",
"│ str ┆ str ┆ str ┆ str ┆ ┆ str ┆ --- ┆ --- ┆ str │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ str ┆ i64 ┆ │\n",
"╞═══════════════╪═══════════════╪═══════════╪════════╪═══╪═══════════════╪═══════════════╪══════════════╪══════════════╡\n",
"│ CS00121500014 ┆ 田崎 美紀 ┆ 1 ┆ 女性 ┆ … ┆ 東京都大田区 ┆ S13001 ┆ 20170605 ┆ 6-20090929-2 │\n",
"│ 5 ┆ ┆ ┆ ┆ ┆ 仲六郷******* ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ *** ┆ ┆ ┆ │\n",
"│ CS03351300018 ┆ 安斎 遥 ┆ 1 ┆ 女性 ┆ … ┆ 神奈川県横浜 ┆ S14033 ┆ 20150728 ┆ 6-20080506-5 │\n",
"│ 0 ┆ ┆ ┆ ┆ ┆ 市旭区善部町* ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ ********* ┆ ┆ ┆ │\n",
"│ CS01121500004 ┆ 芦田 沙耶 ┆ 1 ┆ 女性 ┆ … ┆ 神奈川県横浜 ┆ S14011 ┆ 20150228 ┆ C-20100421-9 │\n",
"│ 8 ┆ ┆ ┆ ┆ ┆ 市港北区日吉 ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ 本町********* ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ * ┆ ┆ ┆ │\n",
"│ CS04041200019 ┆ 川井 郁恵 ┆ 1 ┆ 女性 ┆ … ┆ 神奈川県横浜 ┆ S14040 ┆ 20151101 ┆ 1-20091025-4 │\n",
"│ 1 ┆ ┆ ┆ ┆ ┆ 市緑区北八朔 ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ 町********** ┆ ┆ ┆ │\n",
"│ CS00931500002 ┆ 皆川 文世 ┆ 1 ┆ 女性 ┆ … ┆ 東京都世田谷 ┆ S13009 ┆ 20150319 ┆ 5-20080322-1 │\n",
"│ 3 ┆ ┆ ┆ ┆ ┆ 区駒沢******* ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ *** ┆ ┆ ┆ │\n",
"└───────────────┴───────────────┴───────────┴────────┴───┴───────────────┴───────────────┴──────────────┴──────────────┘"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(\n",
"df_customer\n",
".filter(pl.col.status_cd.str.contains('[1-9]$'))\n",
".head(5)\n",
")"
]
},
{
"cell_type": "markdown",
"id": "928b61ca-c5c8-49f4-b75f-671998181a89",
"metadata": {},
"source": [
"## P-015\n",
"\n",
"顧客データ(df_customer)から、ステータスコード(status_cd)の先頭がアルファベットのA〜Fで始まり、末尾が数字の1〜9で終わるデータを全項目抽出し、10件表示せよ。"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "58abcc6b-3897-4585-aa70-04e02e067ba5",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
shape: (5, 11)customer_id | customer_name | gender_cd | gender | birth_day | age | postal_cd | address | application_store_cd | application_date | status_cd |
---|
str | str | str | str | str | i64 | str | str | str | i64 | str |
"CS011215000048" | "芦田 沙耶" | "1" | "女性" | "1992-02-01" | 27 | "223-0062" | "神奈川県横浜市港北区日吉本町**********" | "S14011" | 20150228 | "C-20100421-9" |
"CS022513000105" | "島村 貴美子" | "1" | "女性" | "1962-03-12" | 57 | "249-0002" | "神奈川県逗子市山の根**********" | "S14022" | 20150320 | "A-20091115-7" |
"CS001515000096" | "水野 陽子" | "9" | "不明" | "1960-11-29" | 58 | "144-0053" | "東京都大田区蒲田本町**********" | "S13001" | 20150614 | "A-20100724-7" |
"CS013615000053" | "西脇 季衣" | "1" | "女性" | "1953-10-18" | 65 | "261-0026" | "千葉県千葉市美浜区幕張西**********" | "S12013" | 20150128 | "B-20100329-6" |
"CS020412000161" | "小宮 薫" | "1" | "女性" | "1974-05-21" | 44 | "174-0042" | "東京都板橋区東坂下**********" | "S13020" | 20150822 | "B-20081021-3" |
"
],
"text/plain": [
"shape: (5, 11)\n",
"┌───────────────┬───────────────┬───────────┬────────┬───┬───────────────┬───────────────┬──────────────┬──────────────┐\n",
"│ customer_id ┆ customer_name ┆ gender_cd ┆ gender ┆ … ┆ address ┆ application_s ┆ application_ ┆ status_cd │\n",
"│ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ tore_cd ┆ date ┆ --- │\n",
"│ str ┆ str ┆ str ┆ str ┆ ┆ str ┆ --- ┆ --- ┆ str │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ str ┆ i64 ┆ │\n",
"╞═══════════════╪═══════════════╪═══════════╪════════╪═══╪═══════════════╪═══════════════╪══════════════╪══════════════╡\n",
"│ CS01121500004 ┆ 芦田 沙耶 ┆ 1 ┆ 女性 ┆ … ┆ 神奈川県横浜 ┆ S14011 ┆ 20150228 ┆ C-20100421-9 │\n",
"│ 8 ┆ ┆ ┆ ┆ ┆ 市港北区日吉 ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ 本町********* ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ * ┆ ┆ ┆ │\n",
"│ CS02251300010 ┆ 島村 貴美子 ┆ 1 ┆ 女性 ┆ … ┆ 神奈川県逗子 ┆ S14022 ┆ 20150320 ┆ A-20091115-7 │\n",
"│ 5 ┆ ┆ ┆ ┆ ┆ 市山の根***** ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ ***** ┆ ┆ ┆ │\n",
"│ CS00151500009 ┆ 水野 陽子 ┆ 9 ┆ 不明 ┆ … ┆ 東京都大田区 ┆ S13001 ┆ 20150614 ┆ A-20100724-7 │\n",
"│ 6 ┆ ┆ ┆ ┆ ┆ 蒲田本町***** ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ ***** ┆ ┆ ┆ │\n",
"│ CS01361500005 ┆ 西脇 季衣 ┆ 1 ┆ 女性 ┆ … ┆ 千葉県千葉市 ┆ S12013 ┆ 20150128 ┆ B-20100329-6 │\n",
"│ 3 ┆ ┆ ┆ ┆ ┆ 美浜区幕張西* ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ ********* ┆ ┆ ┆ │\n",
"│ CS02041200016 ┆ 小宮 薫 ┆ 1 ┆ 女性 ┆ … ┆ 東京都板橋区 ┆ S13020 ┆ 20150822 ┆ B-20081021-3 │\n",
"│ 1 ┆ ┆ ┆ ┆ ┆ 東坂下******* ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ *** ┆ ┆ ┆ │\n",
"└───────────────┴───────────────┴───────────┴────────┴───┴───────────────┴───────────────┴──────────────┴──────────────┘"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(\n",
"df_customer\n",
".filter(pl.col.status_cd.str.contains('^[A-F].*[1-9]$'))\n",
".head(5)\n",
")"
]
},
{
"cell_type": "markdown",
"id": "57b3ab44-774d-4cb4-8f3b-998f418b11f5",
"metadata": {},
"source": [
"## P-016\n",
"\n",
"店舗データ(df_store)から、電話番号(tel_no)が3桁-3桁-4桁のデータを全項目表示せよ。"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "09e8d371-b722-4cbb-aa54-266da112871e",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
shape: (34, 10)store_cd | store_name | prefecture_cd | prefecture | address | address_kana | tel_no | longitude | latitude | floor_area |
---|
str | str | str | str | str | str | str | f64 | f64 | f64 |
"S12014" | "千草台店" | "12" | "千葉県" | "千葉県千葉市稲毛区千草台一丁目" | "チバケンチバシイナゲクチグサダイイッチョウメ" | "043-123-4003" | 140.118 | 35.63559 | 1698.0 |
"S13002" | "国分寺店" | "13" | "東京都" | "東京都国分寺市本多二丁目" | "トウキョウトコクブンジシホンダニチョウメ" | "042-123-4008" | 139.4802 | 35.70566 | 1735.0 |
"S14010" | "菊名店" | "14" | "神奈川県" | "神奈川県横浜市港北区菊名一丁目" | "カナガワケンヨコハマシコウホククキクナイッチョウメ" | "045-123-4032" | 139.6326 | 35.50049 | 1732.0 |
"S14033" | "阿久和店" | "14" | "神奈川県" | "神奈川県横浜市瀬谷区阿久和西一丁目" | "カナガワケンヨコハマシセヤクアクワニシイッチョウメ" | "045-123-4043" | 139.4961 | 35.45918 | 1495.0 |
"S14036" | "相模原中央店" | "14" | "神奈川県" | "神奈川県相模原市中央二丁目" | "カナガワケンサガミハラシチュウオウニチョウメ" | "042-123-4045" | 139.3716 | 35.57327 | 1679.0 |
… | … | … | … | … | … | … | … | … | … |
"S14045" | "厚木店" | "14" | "神奈川県" | "神奈川県厚木市中町二丁目" | "カナガワケンアツギシナカチョウニチョウメ" | "046-123-4048" | 139.3651 | 35.44182 | 980.0 |
"S12029" | "東野店" | "12" | "千葉県" | "千葉県浦安市東野一丁目" | "チバケンウラヤスシヒガシノイッチョウメ" | "047-123-4004" | 139.8968 | 35.65086 | 1101.0 |
"S12053" | "高洲店" | "12" | "千葉県" | "千葉県浦安市高洲五丁目" | "チバケンウラヤスシタカスゴチョウメ" | "047-123-4006" | 139.9176 | 35.63755 | 1555.0 |
"S14024" | "三田店" | "14" | "神奈川県" | "神奈川県川崎市多摩区三田四丁目" | "カナガワケンカワサキシタマクミタヨンチョウメ" | "044-123-4038" | 139.5424 | 35.6077 | 972.0 |
"S14006" | "葛が谷店" | "14" | "神奈川県" | "神奈川県横浜市都筑区葛が谷" | "カナガワケンヨコハマシツヅキククズガヤ" | "045-123-4031" | 139.5633 | 35.53573 | 1886.0 |
"
],
"text/plain": [
"shape: (34, 10)\n",
"┌──────────┬──────────────┬───────────────┬────────────┬───┬──────────────┬───────────┬──────────┬────────────┐\n",
"│ store_cd ┆ store_name ┆ prefecture_cd ┆ prefecture ┆ … ┆ tel_no ┆ longitude ┆ latitude ┆ floor_area │\n",
"│ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- │\n",
"│ str ┆ str ┆ str ┆ str ┆ ┆ str ┆ f64 ┆ f64 ┆ f64 │\n",
"╞══════════╪══════════════╪═══════════════╪════════════╪═══╪══════════════╪═══════════╪══════════╪════════════╡\n",
"│ S12014 ┆ 千草台店 ┆ 12 ┆ 千葉県 ┆ … ┆ 043-123-4003 ┆ 140.118 ┆ 35.63559 ┆ 1698.0 │\n",
"│ S13002 ┆ 国分寺店 ┆ 13 ┆ 東京都 ┆ … ┆ 042-123-4008 ┆ 139.4802 ┆ 35.70566 ┆ 1735.0 │\n",
"│ S14010 ┆ 菊名店 ┆ 14 ┆ 神奈川県 ┆ … ┆ 045-123-4032 ┆ 139.6326 ┆ 35.50049 ┆ 1732.0 │\n",
"│ S14033 ┆ 阿久和店 ┆ 14 ┆ 神奈川県 ┆ … ┆ 045-123-4043 ┆ 139.4961 ┆ 35.45918 ┆ 1495.0 │\n",
"│ S14036 ┆ 相模原中央店 ┆ 14 ┆ 神奈川県 ┆ … ┆ 042-123-4045 ┆ 139.3716 ┆ 35.57327 ┆ 1679.0 │\n",
"│ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … │\n",
"│ S14045 ┆ 厚木店 ┆ 14 ┆ 神奈川県 ┆ … ┆ 046-123-4048 ┆ 139.3651 ┆ 35.44182 ┆ 980.0 │\n",
"│ S12029 ┆ 東野店 ┆ 12 ┆ 千葉県 ┆ … ┆ 047-123-4004 ┆ 139.8968 ┆ 35.65086 ┆ 1101.0 │\n",
"│ S12053 ┆ 高洲店 ┆ 12 ┆ 千葉県 ┆ … ┆ 047-123-4006 ┆ 139.9176 ┆ 35.63755 ┆ 1555.0 │\n",
"│ S14024 ┆ 三田店 ┆ 14 ┆ 神奈川県 ┆ … ┆ 044-123-4038 ┆ 139.5424 ┆ 35.6077 ┆ 972.0 │\n",
"│ S14006 ┆ 葛が谷店 ┆ 14 ┆ 神奈川県 ┆ … ┆ 045-123-4031 ┆ 139.5633 ┆ 35.53573 ┆ 1886.0 │\n",
"└──────────┴──────────────┴───────────────┴────────────┴───┴──────────────┴───────────┴──────────┴────────────┘"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(\n",
"df_store\n",
".filter(pl.col.tel_no.str.contains(r'^\\d{3}-\\d{3}-\\d{4}$'))\n",
")"
]
},
{
"cell_type": "markdown",
"id": "77c9c30a-bb18-4089-a31c-629cb99fe55e",
"metadata": {},
"source": [
"## P-017\n",
"\n",
"顧客データ(df_customer)を生年月日(birth_day)で高齢順にソートし、先頭から全項目を10件表示せよ。"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "774a9026-ff05-4a8d-b1e5-7b3ce384fa9f",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
shape: (5, 11)customer_id | customer_name | gender_cd | gender | birth_day | age | postal_cd | address | application_store_cd | application_date | status_cd |
---|
str | str | str | str | str | i64 | str | str | str | i64 | str |
"CS003813000014" | "村山 菜々美" | "1" | "女性" | "1928-11-26" | 90 | "182-0007" | "東京都調布市菊野台**********" | "S13003" | 20160214 | "0-00000000-0" |
"CS026813000004" | "吉村 朝陽" | "1" | "女性" | "1928-12-14" | 90 | "251-0043" | "神奈川県藤沢市辻堂元町**********" | "S14026" | 20150723 | "0-00000000-0" |
"CS018811000003" | "熊沢 美里" | "1" | "女性" | "1929-01-07" | 90 | "204-0004" | "東京都清瀬市野塩**********" | "S13018" | 20150403 | "0-00000000-0" |
"CS027803000004" | "内村 拓郎" | "0" | "男性" | "1929-01-12" | 90 | "251-0031" | "神奈川県藤沢市鵠沼藤が谷**********" | "S14027" | 20151227 | "0-00000000-0" |
"CS013801000003" | "天野 拓郎" | "0" | "男性" | "1929-01-15" | 90 | "274-0824" | "千葉県船橋市前原東**********" | "S12013" | 20160120 | "0-00000000-0" |
"
],
"text/plain": [
"shape: (5, 11)\n",
"┌───────────────┬───────────────┬───────────┬────────┬───┬───────────────┬───────────────┬──────────────┬──────────────┐\n",
"│ customer_id ┆ customer_name ┆ gender_cd ┆ gender ┆ … ┆ address ┆ application_s ┆ application_ ┆ status_cd │\n",
"│ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ tore_cd ┆ date ┆ --- │\n",
"│ str ┆ str ┆ str ┆ str ┆ ┆ str ┆ --- ┆ --- ┆ str │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ str ┆ i64 ┆ │\n",
"╞═══════════════╪═══════════════╪═══════════╪════════╪═══╪═══════════════╪═══════════════╪══════════════╪══════════════╡\n",
"│ CS00381300001 ┆ 村山 菜々美 ┆ 1 ┆ 女性 ┆ … ┆ 東京都調布市 ┆ S13003 ┆ 20160214 ┆ 0-00000000-0 │\n",
"│ 4 ┆ ┆ ┆ ┆ ┆ 菊野台******* ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ *** ┆ ┆ ┆ │\n",
"│ CS02681300000 ┆ 吉村 朝陽 ┆ 1 ┆ 女性 ┆ … ┆ 神奈川県藤沢 ┆ S14026 ┆ 20150723 ┆ 0-00000000-0 │\n",
"│ 4 ┆ ┆ ┆ ┆ ┆ 市辻堂元町*** ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ ******* ┆ ┆ ┆ │\n",
"│ CS01881100000 ┆ 熊沢 美里 ┆ 1 ┆ 女性 ┆ … ┆ 東京都清瀬市 ┆ S13018 ┆ 20150403 ┆ 0-00000000-0 │\n",
"│ 3 ┆ ┆ ┆ ┆ ┆ 野塩********* ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ * ┆ ┆ ┆ │\n",
"│ CS02780300000 ┆ 内村 拓郎 ┆ 0 ┆ 男性 ┆ … ┆ 神奈川県藤沢 ┆ S14027 ┆ 20151227 ┆ 0-00000000-0 │\n",
"│ 4 ┆ ┆ ┆ ┆ ┆ 市鵠沼藤が谷* ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ ********* ┆ ┆ ┆ │\n",
"│ CS01380100000 ┆ 天野 拓郎 ┆ 0 ┆ 男性 ┆ … ┆ 千葉県船橋市 ┆ S12013 ┆ 20160120 ┆ 0-00000000-0 │\n",
"│ 3 ┆ ┆ ┆ ┆ ┆ 前原東******* ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ *** ┆ ┆ ┆ │\n",
"└───────────────┴───────────────┴───────────┴────────┴───┴───────────────┴───────────────┴──────────────┴──────────────┘"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(\n",
"df_customer\n",
".sort(\n",
" by='birth_day', \n",
" descending=False\n",
")\n",
".head()\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "b8da8dc2-59db-4146-85d6-5580cd2e90ca",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
shape: (5, 3)customer_name | birth_day | age |
---|
str | str | i64 |
"村山 菜々美" | "1928-11-26" | 90 |
"吉村 朝陽" | "1928-12-14" | 90 |
"熊沢 美里" | "1929-01-07" | 90 |
"内村 拓郎" | "1929-01-12" | 90 |
"天野 拓郎" | "1929-01-15" | 90 |
"
],
"text/plain": [
"shape: (5, 3)\n",
"┌───────────────┬────────────┬─────┐\n",
"│ customer_name ┆ birth_day ┆ age │\n",
"│ --- ┆ --- ┆ --- │\n",
"│ str ┆ str ┆ i64 │\n",
"╞═══════════════╪════════════╪═════╡\n",
"│ 村山 菜々美 ┆ 1928-11-26 ┆ 90 │\n",
"│ 吉村 朝陽 ┆ 1928-12-14 ┆ 90 │\n",
"│ 熊沢 美里 ┆ 1929-01-07 ┆ 90 │\n",
"│ 内村 拓郎 ┆ 1929-01-12 ┆ 90 │\n",
"│ 天野 拓郎 ┆ 1929-01-15 ┆ 90 │\n",
"└───────────────┴────────────┴─────┘"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(\n",
"df_customer\n",
".select(\n",
" pl.col('customer_name', 'birth_day', 'age')\n",
" .sort_by('birth_day')\n",
")\n",
".head()\n",
")"
]
},
{
"cell_type": "markdown",
"id": "e0b3678c-0662-4a07-8c9a-fe6c6e35a6ed",
"metadata": {},
"source": [
"## P-018\n",
"\n",
"顧客データ(df_customer)を生年月日(birth_day)で若い順にソートし、先頭から全項目を10件表示せよ。"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "696132b8-f443-4241-b798-4faa53909203",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
shape: (5, 11)customer_id | customer_name | gender_cd | gender | birth_day | age | postal_cd | address | application_store_cd | application_date | status_cd |
---|
str | str | str | str | str | i64 | str | str | str | i64 | str |
"CS035114000004" | "大村 美里" | "1" | "女性" | "2007-11-25" | 11 | "156-0053" | "東京都世田谷区桜**********" | "S13035" | 20150619 | "6-20091205-6" |
"CS022103000002" | "福山 はじめ" | "9" | "不明" | "2007-10-02" | 11 | "249-0006" | "神奈川県逗子市逗子**********" | "S14022" | 20160909 | "0-00000000-0" |
"CS002113000009" | "柴田 真悠子" | "1" | "女性" | "2007-09-17" | 11 | "184-0014" | "東京都小金井市貫井南町**********" | "S13002" | 20160304 | "0-00000000-0" |
"CS004115000014" | "松井 京子" | "1" | "女性" | "2007-08-09" | 11 | "165-0031" | "東京都中野区上鷺宮**********" | "S13004" | 20161120 | "1-20081231-1" |
"CS002114000010" | "山内 遥" | "1" | "女性" | "2007-06-03" | 11 | "184-0015" | "東京都小金井市貫井北町**********" | "S13002" | 20160920 | "6-20100510-1" |
"
],
"text/plain": [
"shape: (5, 11)\n",
"┌───────────────┬───────────────┬───────────┬────────┬───┬───────────────┬───────────────┬──────────────┬──────────────┐\n",
"│ customer_id ┆ customer_name ┆ gender_cd ┆ gender ┆ … ┆ address ┆ application_s ┆ application_ ┆ status_cd │\n",
"│ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ tore_cd ┆ date ┆ --- │\n",
"│ str ┆ str ┆ str ┆ str ┆ ┆ str ┆ --- ┆ --- ┆ str │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ str ┆ i64 ┆ │\n",
"╞═══════════════╪═══════════════╪═══════════╪════════╪═══╪═══════════════╪═══════════════╪══════════════╪══════════════╡\n",
"│ CS03511400000 ┆ 大村 美里 ┆ 1 ┆ 女性 ┆ … ┆ 東京都世田谷 ┆ S13035 ┆ 20150619 ┆ 6-20091205-6 │\n",
"│ 4 ┆ ┆ ┆ ┆ ┆ 区桜********* ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ * ┆ ┆ ┆ │\n",
"│ CS02210300000 ┆ 福山 はじめ ┆ 9 ┆ 不明 ┆ … ┆ 神奈川県逗子 ┆ S14022 ┆ 20160909 ┆ 0-00000000-0 │\n",
"│ 2 ┆ ┆ ┆ ┆ ┆ 市逗子******* ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ *** ┆ ┆ ┆ │\n",
"│ CS00211300000 ┆ 柴田 真悠子 ┆ 1 ┆ 女性 ┆ … ┆ 東京都小金井 ┆ S13002 ┆ 20160304 ┆ 0-00000000-0 │\n",
"│ 9 ┆ ┆ ┆ ┆ ┆ 市貫井南町*** ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ ******* ┆ ┆ ┆ │\n",
"│ CS00411500001 ┆ 松井 京子 ┆ 1 ┆ 女性 ┆ … ┆ 東京都中野区 ┆ S13004 ┆ 20161120 ┆ 1-20081231-1 │\n",
"│ 4 ┆ ┆ ┆ ┆ ┆ 上鷺宮******* ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ *** ┆ ┆ ┆ │\n",
"│ CS00211400001 ┆ 山内 遥 ┆ 1 ┆ 女性 ┆ … ┆ 東京都小金井 ┆ S13002 ┆ 20160920 ┆ 6-20100510-1 │\n",
"│ 0 ┆ ┆ ┆ ┆ ┆ 市貫井北町*** ┆ ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ ******* ┆ ┆ ┆ │\n",
"└───────────────┴───────────────┴───────────┴────────┴───┴───────────────┴───────────────┴──────────────┴──────────────┘"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(\n",
"df_customer\n",
".sort(\n",
" by='birth_day', \n",
" descending=True\n",
")\n",
".head()\n",
")"
]
},
{
"cell_type": "markdown",
"id": "de2fa66e-ec79-4ff1-be82-ed49a692b3a4",
"metadata": {},
"source": [
"## P-019\n",
"\n",
"レシート明細データ(df_receipt)に対し、1件あたりの売上金額(amount)が高い順にランクを付与し、先頭から10件表示せよ。項目は顧客ID(customer_id)、売上金額(amount)、付与したランクを表示させること。なお、売上金額(amount)が等しい場合は同一順位を付与するものとする。"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "5ab8382d-88f8-4336-b832-ca7fa86835ae",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
shape: (10, 3)customer_id | amount | rank |
---|
str | i64 | u32 |
"CS011415000006" | 10925 | 1 |
"ZZ000000000000" | 6800 | 2 |
"CS028605000002" | 5780 | 3 |
"CS015515000034" | 5480 | 4 |
"ZZ000000000000" | 5480 | 4 |
"ZZ000000000000" | 5480 | 4 |
"ZZ000000000000" | 5440 | 7 |
"CS021515000089" | 5440 | 7 |
"CS015515000083" | 5280 | 9 |
"CS017414000114" | 5280 | 9 |
"
],
"text/plain": [
"shape: (10, 3)\n",
"┌────────────────┬────────┬──────┐\n",
"│ customer_id ┆ amount ┆ rank │\n",
"│ --- ┆ --- ┆ --- │\n",
"│ str ┆ i64 ┆ u32 │\n",
"╞════════════════╪════════╪══════╡\n",
"│ CS011415000006 ┆ 10925 ┆ 1 │\n",
"│ ZZ000000000000 ┆ 6800 ┆ 2 │\n",
"│ CS028605000002 ┆ 5780 ┆ 3 │\n",
"│ CS015515000034 ┆ 5480 ┆ 4 │\n",
"│ ZZ000000000000 ┆ 5480 ┆ 4 │\n",
"│ ZZ000000000000 ┆ 5480 ┆ 4 │\n",
"│ ZZ000000000000 ┆ 5440 ┆ 7 │\n",
"│ CS021515000089 ┆ 5440 ┆ 7 │\n",
"│ CS015515000083 ┆ 5280 ┆ 9 │\n",
"│ CS017414000114 ┆ 5280 ┆ 9 │\n",
"└────────────────┴────────┴──────┘"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(\n",
"df_receipt\n",
".select(\n",
" pl.col('customer_id', 'amount')\n",
" .sort_by('amount', descending=True)\n",
")\n",
".with_columns(\n",
" pl.col.amount\n",
" .rank(method='min', descending=True)\n",
" .alias('rank')\n",
")\n",
".head(10)\n",
")"
]
},
{
"cell_type": "markdown",
"id": "f670b0c8-0429-4e37-b625-bc2e1529af97",
"metadata": {},
"source": [
"## P-020\n",
"\n",
"レシート明細データ(df_receipt)に対し、1件あたりの売上金額(amount)が高い順にランクを付与し、先頭から10件表示せよ。項目は顧客ID(customer_id)、売上金額(amount)、付与したランクを表示させること。なお、売上金額(amount)が等しい場合でも別順位を付与すること。"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "3c3d2355-4930-4cc8-beda-aec9d1a2a694",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
shape: (10, 3)customer_id | amount | rank |
---|
str | i64 | u32 |
"CS011415000006" | 10925 | 1 |
"ZZ000000000000" | 6800 | 2 |
"CS028605000002" | 5780 | 3 |
"CS015515000034" | 5480 | 4 |
"ZZ000000000000" | 5480 | 5 |
"ZZ000000000000" | 5480 | 6 |
"ZZ000000000000" | 5440 | 7 |
"CS021515000089" | 5440 | 8 |
"CS015515000083" | 5280 | 9 |
"CS017414000114" | 5280 | 10 |
"
],
"text/plain": [
"shape: (10, 3)\n",
"┌────────────────┬────────┬──────┐\n",
"│ customer_id ┆ amount ┆ rank │\n",
"│ --- ┆ --- ┆ --- │\n",
"│ str ┆ i64 ┆ u32 │\n",
"╞════════════════╪════════╪══════╡\n",
"│ CS011415000006 ┆ 10925 ┆ 1 │\n",
"│ ZZ000000000000 ┆ 6800 ┆ 2 │\n",
"│ CS028605000002 ┆ 5780 ┆ 3 │\n",
"│ CS015515000034 ┆ 5480 ┆ 4 │\n",
"│ ZZ000000000000 ┆ 5480 ┆ 5 │\n",
"│ ZZ000000000000 ┆ 5480 ┆ 6 │\n",
"│ ZZ000000000000 ┆ 5440 ┆ 7 │\n",
"│ CS021515000089 ┆ 5440 ┆ 8 │\n",
"│ CS015515000083 ┆ 5280 ┆ 9 │\n",
"│ CS017414000114 ┆ 5280 ┆ 10 │\n",
"└────────────────┴────────┴──────┘"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(\n",
"df_receipt\n",
".select(\n",
" pl.col('customer_id', 'amount')\n",
" .sort_by('amount', descending=True)\n",
")\n",
".with_columns(\n",
" pl.col.amount\n",
" .rank(method='ordinal', descending=True)\n",
" .alias('rank')\n",
")\n",
".head(10)\n",
")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}