データサイエンス100本ノック 11~20

データサイエンス100本ノック 11~20#

import polars as pl
from helper.polars import load_100knocks_data
pl.Config.set_fmt_str_lengths(100)
df_customer, df_category, df_product, df_receipt, df_store, df_geocode = load_100knocks_data()

P-011#

顧客データ(df_customer)から顧客ID(customer_id)の末尾が1のものだけ全項目抽出し、10件表示せよ。

(
df_customer
.filter(pl.col.customer_id.str.ends_with('1'))
.head(5)
)
shape: (5, 11)
customer_idcustomer_namegender_cdgenderbirth_dayagepostal_cdaddressapplication_store_cdapplication_datestatus_cd
strstrstrstrstri64strstrstri64str
"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"

P-012#

店舗データ(df_store)から、住所 (address) に”横浜市”が含まれるものだけ全項目表示せよ。

(
df_store
.filter(pl.col.address.str.contains("横浜市"))
)
shape: (11, 10)
store_cdstore_nameprefecture_cdprefectureaddressaddress_kanatel_nolongitudelatitudefloor_area
strstrstrstrstrstrstrf64f64f64
"S14010""菊名店""14""神奈川県""神奈川県横浜市港北区菊名一丁目""カナガワケンヨコハマシコウホククキクナイッチョウメ""045-123-4032"139.632635.500491732.0
"S14033""阿久和店""14""神奈川県""神奈川県横浜市瀬谷区阿久和西一丁目""カナガワケンヨコハマシセヤクアクワニシイッチョウメ""045-123-4043"139.496135.459181495.0
"S14040""長津田店""14""神奈川県""神奈川県横浜市緑区長津田みなみ台五丁目""カナガワケンヨコハマシミドリクナガツタミナミダイゴチョウメ""045-123-4046"139.499435.523981548.0
"S14050""阿久和西店""14""神奈川県""神奈川県横浜市瀬谷区阿久和西一丁目""カナガワケンヨコハマシセヤクアクワニシイッチョウメ""045-123-4053"139.496135.459181830.0
"S14028""二ツ橋店""14""神奈川県""神奈川県横浜市瀬谷区二ツ橋町""カナガワケンヨコハマシセヤクフタツバシチョウ""045-123-4042"139.496335.463041574.0
"S14046""北山田店""14""神奈川県""神奈川県横浜市都筑区北山田一丁目""カナガワケンヨコハマシツヅキクキタヤマタイッチョウメ""045-123-4049"139.591635.56189831.0
"S14011""日吉本町店""14""神奈川県""神奈川県横浜市港北区日吉本町四丁目""カナガワケンヨコハマシコウホククヒヨシホンチョウヨンチョウメ""045-123-4033"139.631635.54655890.0
"S14048""中川中央店""14""神奈川県""神奈川県横浜市都筑区中川中央二丁目""カナガワケンヨコハマシツヅキクナカガワチュウオウニチョウメ""045-123-4051"139.575835.549121657.0
"S14042""新山下店""14""神奈川県""神奈川県横浜市中区新山下二丁目""カナガワケンヨコハマシナカクシンヤマシタニチョウメ""045-123-4047"139.659335.438941044.0
"S14006""葛が谷店""14""神奈川県""神奈川県横浜市都筑区葛が谷""カナガワケンヨコハマシツヅキククズガヤ""045-123-4031"139.563335.535731886.0

P-013#

顧客データ(df_customer)から、ステータスコード(status_cd)の先頭がアルファベットのA〜Fで始まるデータを全項目抽出し、10件表示せよ。

(
df_customer
.filter(pl.col.status_cd.str.contains('^[A-F]'))
.head(5)
)
shape: (5, 11)
customer_idcustomer_namegender_cdgenderbirth_dayagepostal_cdaddressapplication_store_cdapplication_datestatus_cd
strstrstrstrstri64strstrstri64str
"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"

P-014#

顧客データ(df_customer)から、ステータスコード(status_cd)の末尾が数字の1〜9で終わるデータを全項目抽出し、10件表示せよ。

(
df_customer
.filter(pl.col.status_cd.str.contains('[1-9]$'))
.head(5)
)
shape: (5, 11)
customer_idcustomer_namegender_cdgenderbirth_dayagepostal_cdaddressapplication_store_cdapplication_datestatus_cd
strstrstrstrstri64strstrstri64str
"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"

P-015#

顧客データ(df_customer)から、ステータスコード(status_cd)の先頭がアルファベットのA〜Fで始まり、末尾が数字の1〜9で終わるデータを全項目抽出し、10件表示せよ。

(
df_customer
.filter(pl.col.status_cd.str.contains('^[A-F].*[1-9]$'))
.head(5)
)
shape: (5, 11)
customer_idcustomer_namegender_cdgenderbirth_dayagepostal_cdaddressapplication_store_cdapplication_datestatus_cd
strstrstrstrstri64strstrstri64str
"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"

P-016#

店舗データ(df_store)から、電話番号(tel_no)が3桁-3桁-4桁のデータを全項目表示せよ。

(
df_store
.filter(pl.col.tel_no.str.contains(r'^\d{3}-\d{3}-\d{4}$'))
)
shape: (34, 10)
store_cdstore_nameprefecture_cdprefectureaddressaddress_kanatel_nolongitudelatitudefloor_area
strstrstrstrstrstrstrf64f64f64
"S12014""千草台店""12""千葉県""千葉県千葉市稲毛区千草台一丁目""チバケンチバシイナゲクチグサダイイッチョウメ""043-123-4003"140.11835.635591698.0
"S13002""国分寺店""13""東京都""東京都国分寺市本多二丁目""トウキョウトコクブンジシホンダニチョウメ""042-123-4008"139.480235.705661735.0
"S14010""菊名店""14""神奈川県""神奈川県横浜市港北区菊名一丁目""カナガワケンヨコハマシコウホククキクナイッチョウメ""045-123-4032"139.632635.500491732.0
"S14033""阿久和店""14""神奈川県""神奈川県横浜市瀬谷区阿久和西一丁目""カナガワケンヨコハマシセヤクアクワニシイッチョウメ""045-123-4043"139.496135.459181495.0
"S14036""相模原中央店""14""神奈川県""神奈川県相模原市中央二丁目""カナガワケンサガミハラシチュウオウニチョウメ""042-123-4045"139.371635.573271679.0
"S14045""厚木店""14""神奈川県""神奈川県厚木市中町二丁目""カナガワケンアツギシナカチョウニチョウメ""046-123-4048"139.365135.44182980.0
"S12029""東野店""12""千葉県""千葉県浦安市東野一丁目""チバケンウラヤスシヒガシノイッチョウメ""047-123-4004"139.896835.650861101.0
"S12053""高洲店""12""千葉県""千葉県浦安市高洲五丁目""チバケンウラヤスシタカスゴチョウメ""047-123-4006"139.917635.637551555.0
"S14024""三田店""14""神奈川県""神奈川県川崎市多摩区三田四丁目""カナガワケンカワサキシタマクミタヨンチョウメ""044-123-4038"139.542435.6077972.0
"S14006""葛が谷店""14""神奈川県""神奈川県横浜市都筑区葛が谷""カナガワケンヨコハマシツヅキククズガヤ""045-123-4031"139.563335.535731886.0

P-017#

顧客データ(df_customer)を生年月日(birth_day)で高齢順にソートし、先頭から全項目を10件表示せよ。

(
df_customer
.sort(
    by='birth_day', 
    descending=False
)
.head()
)
shape: (5, 11)
customer_idcustomer_namegender_cdgenderbirth_dayagepostal_cdaddressapplication_store_cdapplication_datestatus_cd
strstrstrstrstri64strstrstri64str
"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"
(
df_customer
.select(
    pl.col('customer_name', 'birth_day', 'age')
        .sort_by('birth_day')
)
.head()
)
shape: (5, 3)
customer_namebirth_dayage
strstri64
"村山 菜々美""1928-11-26"90
"吉村 朝陽""1928-12-14"90
"熊沢 美里""1929-01-07"90
"内村 拓郎""1929-01-12"90
"天野 拓郎""1929-01-15"90

P-018#

顧客データ(df_customer)を生年月日(birth_day)で若い順にソートし、先頭から全項目を10件表示せよ。

(
df_customer
.sort(
    by='birth_day', 
    descending=True
)
.head()
)
shape: (5, 11)
customer_idcustomer_namegender_cdgenderbirth_dayagepostal_cdaddressapplication_store_cdapplication_datestatus_cd
strstrstrstrstri64strstrstri64str
"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"

P-019#

レシート明細データ(df_receipt)に対し、1件あたりの売上金額(amount)が高い順にランクを付与し、先頭から10件表示せよ。項目は顧客ID(customer_id)、売上金額(amount)、付与したランクを表示させること。なお、売上金額(amount)が等しい場合は同一順位を付与するものとする。

(
df_receipt
.select(
    pl.col('customer_id', 'amount')
        .sort_by('amount', descending=True)
)
.with_columns(
    pl.col.amount
        .rank(method='min', descending=True)
        .alias('rank')
)
.head(10)
)
shape: (10, 3)
customer_idamountrank
stri64u32
"CS011415000006"109251
"ZZ000000000000"68002
"CS028605000002"57803
"CS015515000034"54804
"ZZ000000000000"54804
"ZZ000000000000"54804
"ZZ000000000000"54407
"CS021515000089"54407
"CS015515000083"52809
"CS017414000114"52809

P-020#

レシート明細データ(df_receipt)に対し、1件あたりの売上金額(amount)が高い順にランクを付与し、先頭から10件表示せよ。項目は顧客ID(customer_id)、売上金額(amount)、付与したランクを表示させること。なお、売上金額(amount)が等しい場合でも別順位を付与すること。

(
df_receipt
.select(
    pl.col('customer_id', 'amount')
        .sort_by('amount', descending=True)
)
.with_columns(
    pl.col.amount
        .rank(method='ordinal', descending=True)
        .alias('rank')
)
.head(10)
)
shape: (10, 3)
customer_idamountrank
stri64u32
"CS011415000006"109251
"ZZ000000000000"68002
"CS028605000002"57803
"CS015515000034"54804
"ZZ000000000000"54805
"ZZ000000000000"54806
"ZZ000000000000"54407
"CS021515000089"54408
"CS015515000083"52809
"CS017414000114"528010