データサイエンス100本ノック（構造化データ加工編） - Julia/Base/Query

データサイエンス100本ノック（構造化データ加工編） - Julia/BaseR/Query

別稿では DataFramesMeta を使う場合の解答例を示した。
https://qiita.com/WolfMoon/items/387d18e4ea3f65768997
本稿では BaseR の場合と Query を使う場合の解答例を示す。


# ファイル等の準備

https://github.com/The-Japan-DataScientist-Society/100knocks-preprocess
に書かれているように進めればよいが，デスクトップマシンに Python の実行環境がある場合には，そのページの中程の Requirement > Cloud Service > Colaboratory > Open in Colab をクリックし，最初のセルの

`import os`<br>
`import pandas as pd`<br>
  `:`<br>
`df_geocode = pd.read_csv("data/geocode.csv", dtype=dtype)`

を実行すれば，作業ディレクトリの下に `100knocks-preprocess/docker/work/data/` というディレクトリができ，これをベースディレクトリとして以下のように 6 個のデータファイルを読み込めば，始めることができる。

なお，それぞれのファイルの列を数値として読み込むか文字列として読み込むかはユーザが指定しなければならないことがある。入力結果を確認し，想定と違う場合には読み込み方を指示しなければならない。Julia では CSV.read() の types 引数で指定する。

```julia
using DataFrames
using CSV
using Query

basedirectory = "../Python/100knocks-preprocess/docker/work/data/"
df_customer = CSV.read(basedirectory * "customer.csv", DataFrame, types=Dict(3 => String, 10 => String));
df_category = CSV.read(basedirectory * "category.csv", DataFrame, types=String);
df_product = CSV.read(basedirectory * "product.csv", DataFrame, types=Dict(2 => String, 3 => String, 4 => String));
df_receipt = CSV.read(basedirectory * "receipt.csv", DataFrame);
df_store = CSV.read(basedirectory * "store.csv", DataFrame, types=Dict(3 => String));
df_geocode = CSV.read(basedirectory * "geocode.csv", DataFrame);
```

はじめに
- ~~初めに以下のセルを実行してください~~
- ~~必要なライブラリのインポートとデータベース（PostgreSQL）からのデータ読み込みを行います~~
- ~~pandas等、利用が想定されるライブラリは以下セルでインポートしています~~
- ~~その他利用したいライブラリがあれば適宜インストールしてください（"!pip install ライブラリ名"でインストールも可能）~~
- 処理は複数回に分けても構いません
- 名前、住所等はダミーデータであり、実在するものではありません

# 入力したデータフレームの確認

**入力後のデータフレームの確認は，ここでは先頭 10 件を表示させ，目視で確認するということであるが，それぞれの列がどのように読み取られているかを確認すべきである。**

**実際には，各項目の度数分布を求めたり，ヒストグラムを描いたりして不適切なデータがあるかないかを確認すべきである。また，欠損値の有無と欠損値に特別の値が与えられているかどうかなどを確認しておく必要がある。**

## 1.　単純な目視確認

---
> J-001: レシート明細データ（df_receipt）から全項目の先頭10件を表示し、どのようなデータを保有しているか目視で確認せよ。

```julia
df_receipt[1:10, :]  # 範囲を指定できる
```

<div><div style = "float: left;"><span>10×9 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">sales_epoch</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">receipt_no</th><th style = "text-align: left;">receipt_sub_no</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">quantity</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20181103</td><td style = "text-align: right;">1541203200</td><td style = "text-align: left;">S14006</td><td style = "text-align: right;">112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS006214000001</td><td style = "text-align: left;">P070305012</td><td style = "text-align: right;">1</td><td style = "text-align: right;">158</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20181118</td><td style = "text-align: right;">1542499200</td><td style = "text-align: left;">S13008</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">2</td><td style = "text-align: left;">CS008415000097</td><td style = "text-align: left;">P070701017</td><td style = "text-align: right;">1</td><td style = "text-align: right;">81</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170712</td><td style = "text-align: right;">1499817600</td><td style = "text-align: left;">S14028</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS028414000014</td><td style = "text-align: left;">P060101005</td><td style = "text-align: right;">1</td><td style = "text-align: right;">170</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20190205</td><td style = "text-align: right;">1549324800</td><td style = "text-align: left;">S14042</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">1</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: left;">P050301001</td><td style = "text-align: right;">1</td><td style = "text-align: right;">25</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20180821</td><td style = "text-align: right;">1534809600</td><td style = "text-align: left;">S14025</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">CS025415000050</td><td style = "text-align: left;">P060102007</td><td style = "text-align: right;">1</td><td style = "text-align: right;">90</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">20190605</td><td style = "text-align: right;">1559692800</td><td style = "text-align: left;">S13003</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS003515000195</td><td style = "text-align: left;">P050102002</td><td style = "text-align: right;">1</td><td style = "text-align: right;">138</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">20181205</td><td style = "text-align: right;">1543968000</td><td style = "text-align: left;">S14024</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">CS024514000042</td><td style = "text-align: left;">P080101005</td><td style = "text-align: right;">1</td><td style = "text-align: right;">30</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">20190922</td><td style = "text-align: right;">1569110400</td><td style = "text-align: left;">S14040</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS040415000178</td><td style = "text-align: left;">P070501004</td><td style = "text-align: right;">1</td><td style = "text-align: right;">128</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">20170504</td><td style = "text-align: right;">1493856000</td><td style = "text-align: left;">S13020</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">2</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: left;">P071302010</td><td style = "text-align: right;">1</td><td style = "text-align: right;">770</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">20191010</td><td style = "text-align: right;">1570665600</td><td style = "text-align: left;">S14027</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS027514000015</td><td style = "text-align: left;">P071101003</td><td style = "text-align: right;">1</td><td style = "text-align: right;">680</td></tr></tbody></table></div>

```julia
# 別解
first(df_receipt, 10)  # 特定の処理に特化したの関数が用意されていることもある
```

<div><div style = "float: left;"><span>10×9 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">sales_epoch</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">receipt_no</th><th style = "text-align: left;">receipt_sub_no</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">quantity</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20181103</td><td style = "text-align: right;">1541203200</td><td style = "text-align: left;">S14006</td><td style = "text-align: right;">112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS006214000001</td><td style = "text-align: left;">P070305012</td><td style = "text-align: right;">1</td><td style = "text-align: right;">158</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20181118</td><td style = "text-align: right;">1542499200</td><td style = "text-align: left;">S13008</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">2</td><td style = "text-align: left;">CS008415000097</td><td style = "text-align: left;">P070701017</td><td style = "text-align: right;">1</td><td style = "text-align: right;">81</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170712</td><td style = "text-align: right;">1499817600</td><td style = "text-align: left;">S14028</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS028414000014</td><td style = "text-align: left;">P060101005</td><td style = "text-align: right;">1</td><td style = "text-align: right;">170</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20190205</td><td style = "text-align: right;">1549324800</td><td style = "text-align: left;">S14042</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">1</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: left;">P050301001</td><td style = "text-align: right;">1</td><td style = "text-align: right;">25</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20180821</td><td style = "text-align: right;">1534809600</td><td style = "text-align: left;">S14025</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">CS025415000050</td><td style = "text-align: left;">P060102007</td><td style = "text-align: right;">1</td><td style = "text-align: right;">90</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">20190605</td><td style = "text-align: right;">1559692800</td><td style = "text-align: left;">S13003</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS003515000195</td><td style = "text-align: left;">P050102002</td><td style = "text-align: right;">1</td><td style = "text-align: right;">138</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">20181205</td><td style = "text-align: right;">1543968000</td><td style = "text-align: left;">S14024</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">CS024514000042</td><td style = "text-align: left;">P080101005</td><td style = "text-align: right;">1</td><td style = "text-align: right;">30</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">20190922</td><td style = "text-align: right;">1569110400</td><td style = "text-align: left;">S14040</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS040415000178</td><td style = "text-align: left;">P070501004</td><td style = "text-align: right;">1</td><td style = "text-align: right;">128</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">20170504</td><td style = "text-align: right;">1493856000</td><td style = "text-align: left;">S13020</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">2</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: left;">P071302010</td><td style = "text-align: right;">1</td><td style = "text-align: right;">770</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">20191010</td><td style = "text-align: right;">1570665600</td><td style = "text-align: left;">S14027</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS027514000015</td><td style = "text-align: left;">P071101003</td><td style = "text-align: right;">1</td><td style = "text-align: right;">680</td></tr></tbody></table></div>

**Query，パイプを使うことを好む人もいるが，多くの場合面倒くさい。以下しばらくの間，パイプと組み合わせて Query を使う（R の tidyverse のような）やり方を別解として記しておく。**

```julia
# 別解2
df_receipt |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×9 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">sales_epoch</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">receipt_no</th><th style = "text-align: left;">receipt_sub_no</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">quantity</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20181103</td><td style = "text-align: right;">1541203200</td><td style = "text-align: left;">S14006</td><td style = "text-align: right;">112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS006214000001</td><td style = "text-align: left;">P070305012</td><td style = "text-align: right;">1</td><td style = "text-align: right;">158</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20181118</td><td style = "text-align: right;">1542499200</td><td style = "text-align: left;">S13008</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">2</td><td style = "text-align: left;">CS008415000097</td><td style = "text-align: left;">P070701017</td><td style = "text-align: right;">1</td><td style = "text-align: right;">81</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170712</td><td style = "text-align: right;">1499817600</td><td style = "text-align: left;">S14028</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS028414000014</td><td style = "text-align: left;">P060101005</td><td style = "text-align: right;">1</td><td style = "text-align: right;">170</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20190205</td><td style = "text-align: right;">1549324800</td><td style = "text-align: left;">S14042</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">1</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: left;">P050301001</td><td style = "text-align: right;">1</td><td style = "text-align: right;">25</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20180821</td><td style = "text-align: right;">1534809600</td><td style = "text-align: left;">S14025</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">CS025415000050</td><td style = "text-align: left;">P060102007</td><td style = "text-align: right;">1</td><td style = "text-align: right;">90</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">20190605</td><td style = "text-align: right;">1559692800</td><td style = "text-align: left;">S13003</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS003515000195</td><td style = "text-align: left;">P050102002</td><td style = "text-align: right;">1</td><td style = "text-align: right;">138</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">20181205</td><td style = "text-align: right;">1543968000</td><td style = "text-align: left;">S14024</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">CS024514000042</td><td style = "text-align: left;">P080101005</td><td style = "text-align: right;">1</td><td style = "text-align: right;">30</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">20190922</td><td style = "text-align: right;">1569110400</td><td style = "text-align: left;">S14040</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS040415000178</td><td style = "text-align: left;">P070501004</td><td style = "text-align: right;">1</td><td style = "text-align: right;">128</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">20170504</td><td style = "text-align: right;">1493856000</td><td style = "text-align: left;">S13020</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">2</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: left;">P071302010</td><td style = "text-align: right;">1</td><td style = "text-align: right;">770</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">20191010</td><td style = "text-align: right;">1570665600</td><td style = "text-align: left;">S14027</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS027514000015</td><td style = "text-align: left;">P071101003</td><td style = "text-align: right;">1</td><td style = "text-align: right;">680</td></tr></tbody></table></div>

## 2.　必要な項目（列）のみを表示する

---
> J-002: レシート明細データ（df_receipt）から売上年月日（sales_ymd）、顧客ID（customer_id）、商品コード（product_cd）、売上金額（amount）の順に列を指定し、10件表示せよ。

```julia
df_receipt[1:10, [:sales_ymd, :customer_id, :product_cd, :amount]]  # 行と列を指定できる（順序も）
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20181103</td><td style = "text-align: left;">CS006214000001</td><td style = "text-align: left;">P070305012</td><td style = "text-align: right;">158</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20181118</td><td style = "text-align: left;">CS008415000097</td><td style = "text-align: left;">P070701017</td><td style = "text-align: right;">81</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170712</td><td style = "text-align: left;">CS028414000014</td><td style = "text-align: left;">P060101005</td><td style = "text-align: right;">170</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20190205</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: left;">P050301001</td><td style = "text-align: right;">25</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20180821</td><td style = "text-align: left;">CS025415000050</td><td style = "text-align: left;">P060102007</td><td style = "text-align: right;">90</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">20190605</td><td style = "text-align: left;">CS003515000195</td><td style = "text-align: left;">P050102002</td><td style = "text-align: right;">138</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">20181205</td><td style = "text-align: left;">CS024514000042</td><td style = "text-align: left;">P080101005</td><td style = "text-align: right;">30</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">20190922</td><td style = "text-align: left;">CS040415000178</td><td style = "text-align: left;">P070501004</td><td style = "text-align: right;">128</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">20170504</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: left;">P071302010</td><td style = "text-align: right;">770</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">20191010</td><td style = "text-align: left;">CS027514000015</td><td style = "text-align: left;">P071101003</td><td style = "text-align: right;">680</td></tr></tbody></table></div>

```julia
# 別解
df = df_receipt[!, [:sales_ymd, :customer_id, :product_cd, :amount]]
first(df, 10)
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20181103</td><td style = "text-align: left;">CS006214000001</td><td style = "text-align: left;">P070305012</td><td style = "text-align: right;">158</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20181118</td><td style = "text-align: left;">CS008415000097</td><td style = "text-align: left;">P070701017</td><td style = "text-align: right;">81</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170712</td><td style = "text-align: left;">CS028414000014</td><td style = "text-align: left;">P060101005</td><td style = "text-align: right;">170</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20190205</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: left;">P050301001</td><td style = "text-align: right;">25</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20180821</td><td style = "text-align: left;">CS025415000050</td><td style = "text-align: left;">P060102007</td><td style = "text-align: right;">90</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">20190605</td><td style = "text-align: left;">CS003515000195</td><td style = "text-align: left;">P050102002</td><td style = "text-align: right;">138</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">20181205</td><td style = "text-align: left;">CS024514000042</td><td style = "text-align: left;">P080101005</td><td style = "text-align: right;">30</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">20190922</td><td style = "text-align: left;">CS040415000178</td><td style = "text-align: left;">P070501004</td><td style = "text-align: right;">128</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">20170504</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: left;">P071302010</td><td style = "text-align: right;">770</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">20191010</td><td style = "text-align: left;">CS027514000015</td><td style = "text-align: left;">P071101003</td><td style = "text-align: right;">680</td></tr></tbody></table></div>

**df[] の中の最初の次元の ! または : はすべての行を意味する。! は直接参照するが，: はコピーしたものを参照する。そのため，! を使うほうが処理時間は短い。**

```julia
@time df = df_receipt[!, [:sales_ymd, :customer_id, :product_cd, :amount]];
```

      0.000022 seconds (24 allocations: 1.875 KiB)

```julia
@time df = df_receipt[:, [:sales_ymd, :customer_id, :product_cd, :amount]];
```

      0.004656 seconds (11.01 k allocations: 5.386 MiB, 88.53% compilation time)

```julia
# 別解2
df_receipt |>
    @select(:sales_ymd, :customer_id, :product_cd, :amount) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20181103</td><td style = "text-align: left;">CS006214000001</td><td style = "text-align: left;">P070305012</td><td style = "text-align: right;">158</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20181118</td><td style = "text-align: left;">CS008415000097</td><td style = "text-align: left;">P070701017</td><td style = "text-align: right;">81</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170712</td><td style = "text-align: left;">CS028414000014</td><td style = "text-align: left;">P060101005</td><td style = "text-align: right;">170</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20190205</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: left;">P050301001</td><td style = "text-align: right;">25</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20180821</td><td style = "text-align: left;">CS025415000050</td><td style = "text-align: left;">P060102007</td><td style = "text-align: right;">90</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">20190605</td><td style = "text-align: left;">CS003515000195</td><td style = "text-align: left;">P050102002</td><td style = "text-align: right;">138</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">20181205</td><td style = "text-align: left;">CS024514000042</td><td style = "text-align: left;">P080101005</td><td style = "text-align: right;">30</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">20190922</td><td style = "text-align: left;">CS040415000178</td><td style = "text-align: left;">P070501004</td><td style = "text-align: right;">128</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">20170504</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: left;">P071302010</td><td style = "text-align: right;">770</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">20191010</td><td style = "text-align: left;">CS027514000015</td><td style = "text-align: left;">P071101003</td><td style = "text-align: right;">680</td></tr></tbody></table></div>

---
> J-003: レシート明細データ（df_receipt）から売上年月日（sales_ymd）、顧客ID（customer_id）、商品コード（product_cd）、売上金額（amount）の順に列を指定し、10件表示せよ。ただし、sales_ymdをsales_dateに項目名を変更しながら抽出すること。

```julia
df = df_receipt[!, [:sales_ymd, :customer_id, :product_cd, :amount]]
df = rename(df, :sales_ymd => :sales_date)  # rename!() を使うときには注意
first(df, 10)
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_date</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20181103</td><td style = "text-align: left;">CS006214000001</td><td style = "text-align: left;">P070305012</td><td style = "text-align: right;">158</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20181118</td><td style = "text-align: left;">CS008415000097</td><td style = "text-align: left;">P070701017</td><td style = "text-align: right;">81</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170712</td><td style = "text-align: left;">CS028414000014</td><td style = "text-align: left;">P060101005</td><td style = "text-align: right;">170</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20190205</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: left;">P050301001</td><td style = "text-align: right;">25</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20180821</td><td style = "text-align: left;">CS025415000050</td><td style = "text-align: left;">P060102007</td><td style = "text-align: right;">90</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">20190605</td><td style = "text-align: left;">CS003515000195</td><td style = "text-align: left;">P050102002</td><td style = "text-align: right;">138</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">20181205</td><td style = "text-align: left;">CS024514000042</td><td style = "text-align: left;">P080101005</td><td style = "text-align: right;">30</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">20190922</td><td style = "text-align: left;">CS040415000178</td><td style = "text-align: left;">P070501004</td><td style = "text-align: right;">128</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">20170504</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: left;">P071302010</td><td style = "text-align: right;">770</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">20191010</td><td style = "text-align: left;">CS027514000015</td><td style = "text-align: left;">P071101003</td><td style = "text-align: right;">680</td></tr></tbody></table></div>

```julia
# 別解
df_receipt |>
    @select(:sales_ymd, :customer_id, :product_cd, :amount) |>
    @rename(:sales_ymd => :sales_date) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_date</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20181103</td><td style = "text-align: left;">CS006214000001</td><td style = "text-align: left;">P070305012</td><td style = "text-align: right;">158</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20181118</td><td style = "text-align: left;">CS008415000097</td><td style = "text-align: left;">P070701017</td><td style = "text-align: right;">81</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170712</td><td style = "text-align: left;">CS028414000014</td><td style = "text-align: left;">P060101005</td><td style = "text-align: right;">170</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20190205</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: left;">P050301001</td><td style = "text-align: right;">25</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20180821</td><td style = "text-align: left;">CS025415000050</td><td style = "text-align: left;">P060102007</td><td style = "text-align: right;">90</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">20190605</td><td style = "text-align: left;">CS003515000195</td><td style = "text-align: left;">P050102002</td><td style = "text-align: right;">138</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">20181205</td><td style = "text-align: left;">CS024514000042</td><td style = "text-align: left;">P080101005</td><td style = "text-align: right;">30</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">20190922</td><td style = "text-align: left;">CS040415000178</td><td style = "text-align: left;">P070501004</td><td style = "text-align: right;">128</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">20170504</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: left;">P071302010</td><td style = "text-align: right;">770</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">20191010</td><td style = "text-align: left;">CS027514000015</td><td style = "text-align: left;">P071101003</td><td style = "text-align: right;">680</td></tr></tbody></table></div>

# 行と列の選択

## 3.　論理演算式による行の選択

---
> J-004: レシート明細データ（df_receipt）から売上日（sales_ymd）、顧客ID（customer_id）、商品コード（product_cd）、売上金額（amount）の順に列を指定し、以下の条件を満たすデータを抽出せよ。
> - 顧客ID（customer_id）が"CS018205000001"

* **列の選択はシンボルベクトル（:foo など）で行う。**
    * **指定した順で選択される。**
    * **1 列だけを指定するときもベクトルで指定する（[:foo] のように）。**
* **行を選択するための条件式（論理式）を書くことができる。**
    * **条件式中の演算子は通常は先行するドット（.）が必要である。**
    * **条件式中の列 :foo の指定は df.foo のようにする。**

```julia
df_receipt[df_receipt.customer_id .== "CS018205000001", [:sales_ymd, :customer_id, :product_cd, :amount]]
```

<div><div style = "float: left;"><span>12×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401012</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20180414</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P060104007</td><td style = "text-align: right;">600</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170614</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P050206001</td><td style = "text-align: right;">990</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20170614</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P060702015</td><td style = "text-align: right;">108</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20190216</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071005024</td><td style = "text-align: right;">102</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">20180414</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071101002</td><td style = "text-align: right;">278</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">20190226</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P070902035</td><td style = "text-align: right;">168</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">20190924</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P060805001</td><td style = "text-align: right;">495</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">20190226</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401020</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401005</td><td style = "text-align: right;">1100</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">11</td><td style = "text-align: right;">20190216</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P040101002</td><td style = "text-align: right;">218</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">12</td><td style = "text-align: right;">20190924</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P091503001</td><td style = "text-align: right;">280</td></tr></tbody></table></div>

```julia
# 別解
df_receipt |>
    @select(:sales_ymd, :customer_id, :product_cd, :amount) |>
    @filter(_.customer_id == "CS018205000001") |>
    DataFrame
```

<div><div style = "float: left;"><span>12×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401012</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20180414</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P060104007</td><td style = "text-align: right;">600</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170614</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P050206001</td><td style = "text-align: right;">990</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20170614</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P060702015</td><td style = "text-align: right;">108</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20190216</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071005024</td><td style = "text-align: right;">102</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">20180414</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071101002</td><td style = "text-align: right;">278</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">20190226</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P070902035</td><td style = "text-align: right;">168</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">20190924</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P060805001</td><td style = "text-align: right;">495</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">20190226</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401020</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401005</td><td style = "text-align: right;">1100</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">11</td><td style = "text-align: right;">20190216</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P040101002</td><td style = "text-align: right;">218</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">12</td><td style = "text-align: right;">20190924</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P091503001</td><td style = "text-align: right;">280</td></tr></tbody></table></div>

---
> J-005: レシート明細データ（df_receipt）から売上日（sales_ymd）、顧客ID（customer_id）、商品コード（product_cd）、売上金額（amount）の順に列を指定し、以下の全ての条件を満たすデータを抽出せよ。
> - 顧客ID（customer_id）が"CS018205000001"
> - 売上金額（amount）が1,000以上

**複雑な論理式を書くこともできる。**

```julia
df_receipt[df_receipt.customer_id .== "CS018205000001" .&& df_receipt.amount .>= 1000, [:sales_ymd, :customer_id, :product_cd, :amount]]
```

<div><div style = "float: left;"><span>3×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401012</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20190226</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401020</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401005</td><td style = "text-align: right;">1100</td></tr></tbody></table></div>

```julia
# 別解
df = select(df_receipt, [:sales_ymd, :customer_id, :product_cd, :amount])
filter([:customer_id, :amount] => (x, y) -> x == "CS018205000001" && y >= 1000, df)
```

<div><div style = "float: left;"><span>3×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401012</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20190226</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401020</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401005</td><td style = "text-align: right;">1100</td></tr></tbody></table></div>

```julia
# 別解2
df_receipt |>
    @select(:sales_ymd, :customer_id, :product_cd, :amount) |>
    @filter(_.customer_id == "CS018205000001" && _.amount >= 1000) |>
    DataFrame
```

<div><div style = "float: left;"><span>3×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401012</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20190226</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401020</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401005</td><td style = "text-align: right;">1100</td></tr></tbody></table></div>

---
> J-006: レシート明細データ（df_receipt）から売上日（sales_ymd）、顧客ID（customer_id）、商品コード（product_cd）、売上数量（quantity）、売上金額（amount）の順に列を指定し、以下の全ての条件を満たすデータを抽出せよ。
> - 顧客ID（customer_id）が"CS018205000001"
> - 売上金額（amount）が1,000以上または売上数量（quantity）が5以上

```julia
df_receipt[df_receipt.customer_id .== "CS018205000001" .&& (df_receipt.amount .>= 1000 .|| df_receipt.quantity .>= 5), [:sales_ymd, :customer_id, :product_cd, :quantity, :amount]]
```

<div><div style = "float: left;"><span>5×5 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">quantity</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401012</td><td style = "text-align: right;">1</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20180414</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P060104007</td><td style = "text-align: right;">6</td><td style = "text-align: right;">600</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170614</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P050206001</td><td style = "text-align: right;">5</td><td style = "text-align: right;">990</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20190226</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401020</td><td style = "text-align: right;">1</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401005</td><td style = "text-align: right;">1</td><td style = "text-align: right;">1100</td></tr></tbody></table></div>

```julia
# 別解
df = select(df_receipt, [:sales_ymd, :customer_id, :product_cd, :quantity, :amount])
filter([:customer_id, :amount, :quantity] => (x, y, z) -> x == "CS018205000001" && (y >= 1000 || z >= 5), df)
```

<div><div style = "float: left;"><span>5×5 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">quantity</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401012</td><td style = "text-align: right;">1</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20180414</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P060104007</td><td style = "text-align: right;">6</td><td style = "text-align: right;">600</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170614</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P050206001</td><td style = "text-align: right;">5</td><td style = "text-align: right;">990</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20190226</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401020</td><td style = "text-align: right;">1</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401005</td><td style = "text-align: right;">1</td><td style = "text-align: right;">1100</td></tr></tbody></table></div>

```julia
# 別解2
df_receipt |>
    @select(:sales_ymd, :customer_id, :product_cd, :quantity, :amount) |>
    @filter(_.customer_id == "CS018205000001" && (_.amount >= 1000 || _.quantity >= 5)) |>
    DataFrame
```

<div><div style = "float: left;"><span>5×5 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">quantity</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401012</td><td style = "text-align: right;">1</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20180414</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P060104007</td><td style = "text-align: right;">6</td><td style = "text-align: right;">600</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170614</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P050206001</td><td style = "text-align: right;">5</td><td style = "text-align: right;">990</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20190226</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401020</td><td style = "text-align: right;">1</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401005</td><td style = "text-align: right;">1</td><td style = "text-align: right;">1100</td></tr></tbody></table></div>

---
> J-007: レシート明細データ（df_receipt）から売上日（sales_ymd）、顧客ID（customer_id）、商品コード（product_cd）、売上金額（amount）の順に列を指定し、以下の全ての条件を満たすデータを抽出せよ。
> - 顧客ID（customer_id）が"CS018205000001"
> - 売上金額（amount）が1,000以上2,000以下

```julia
df_receipt[df_receipt.customer_id .== "CS018205000001" .&& 1000 .<= df_receipt.amount .<= 2000, [:sales_ymd, :customer_id, :product_cd, :amount]]
```

<div><div style = "float: left;"><span>1×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401005</td><td style = "text-align: right;">1100</td></tr></tbody></table></div>

```julia
# 別解
df = select(df_receipt, [:sales_ymd, :customer_id, :product_cd, :amount])
filter([:customer_id, :amount] => (x, y) -> x == "CS018205000001" && y >= 1000 <= y <= 2000, df)
```

<div><div style = "float: left;"><span>1×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401005</td><td style = "text-align: right;">1100</td></tr></tbody></table></div>

```julia
# 別解2
df_receipt |>
    @select(:sales_ymd, :customer_id, :product_cd, :amount) |>
    @filter(_.customer_id == "CS018205000001" && 1000 <= _.amount <= 2000) |>
    DataFrame
```

<div><div style = "float: left;"><span>1×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401005</td><td style = "text-align: right;">1100</td></tr></tbody></table></div>

---
> J-008: レシート明細データ（df_receipt）から売上日（sales_ymd）、顧客ID（customer_id）、商品コード（product_cd）、売上金額（amount）の順に列を指定し、以下の全ての条件を満たすデータを抽出せよ。
> - 顧客ID（customer_id）が"CS018205000001"
> - 商品コード（product_cd）が"P071401019"以外

```julia
df_receipt[df_receipt.customer_id .== "CS018205000001" .&& df_receipt.product_cd .!= "P071401019", [:sales_ymd, :customer_id, :product_cd, :amount]]
```

<div><div style = "float: left;"><span>12×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401012</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20180414</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P060104007</td><td style = "text-align: right;">600</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170614</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P050206001</td><td style = "text-align: right;">990</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20170614</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P060702015</td><td style = "text-align: right;">108</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20190216</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071005024</td><td style = "text-align: right;">102</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">20180414</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071101002</td><td style = "text-align: right;">278</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">20190226</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P070902035</td><td style = "text-align: right;">168</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">20190924</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P060805001</td><td style = "text-align: right;">495</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">20190226</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401020</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401005</td><td style = "text-align: right;">1100</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">11</td><td style = "text-align: right;">20190216</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P040101002</td><td style = "text-align: right;">218</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">12</td><td style = "text-align: right;">20190924</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P091503001</td><td style = "text-align: right;">280</td></tr></tbody></table></div>

```julia
# 別解
df = select(df_receipt, [:sales_ymd, :customer_id, :product_cd, :amount])
filter([:customer_id, :product_cd] => (x, y) -> x == "CS018205000001" && y != "P071401019", df)
```

<div><div style = "float: left;"><span>12×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401012</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20180414</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P060104007</td><td style = "text-align: right;">600</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170614</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P050206001</td><td style = "text-align: right;">990</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20170614</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P060702015</td><td style = "text-align: right;">108</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20190216</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071005024</td><td style = "text-align: right;">102</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">20180414</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071101002</td><td style = "text-align: right;">278</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">20190226</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P070902035</td><td style = "text-align: right;">168</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">20190924</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P060805001</td><td style = "text-align: right;">495</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">20190226</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401020</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401005</td><td style = "text-align: right;">1100</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">11</td><td style = "text-align: right;">20190216</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P040101002</td><td style = "text-align: right;">218</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">12</td><td style = "text-align: right;">20190924</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P091503001</td><td style = "text-align: right;">280</td></tr></tbody></table></div>

```julia
# 別解2
df_receipt |>
    @select(:sales_ymd, :customer_id, :product_cd, :amount) |>
    @filter(_.customer_id == "CS018205000001" && _.product_cd != "P071401019") |>
    DataFrame
```

<div><div style = "float: left;"><span>12×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401012</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20180414</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P060104007</td><td style = "text-align: right;">600</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170614</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P050206001</td><td style = "text-align: right;">990</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20170614</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P060702015</td><td style = "text-align: right;">108</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20190216</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071005024</td><td style = "text-align: right;">102</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">20180414</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071101002</td><td style = "text-align: right;">278</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">20190226</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P070902035</td><td style = "text-align: right;">168</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">20190924</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P060805001</td><td style = "text-align: right;">495</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">20190226</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401020</td><td style = "text-align: right;">2200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">20180911</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P071401005</td><td style = "text-align: right;">1100</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">11</td><td style = "text-align: right;">20190216</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P040101002</td><td style = "text-align: right;">218</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">12</td><td style = "text-align: right;">20190924</td><td style = "text-align: left;">CS018205000001</td><td style = "text-align: left;">P091503001</td><td style = "text-align: right;">280</td></tr></tbody></table></div>

---
> J-009: 以下の処理において、出力結果を変えずにORをANDに書き換えよ。
> 
> `df_store.query('not(prefecture_cd == "13" | floor_area > 900)')`

```julia
df_store[df_store.prefecture_cd .!= "13" .&& df_store.floor_area .<= 900, :]
```

<div><div style = "float: left;"><span>3×10 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">store_name</th><th style = "text-align: left;">prefecture_cd</th><th style = "text-align: left;">prefecture</th><th style = "text-align: left;">address</th><th style = "text-align: left;">address_kana</th><th style = "text-align: left;">tel_no</th><th style = "text-align: left;">longitude</th><th style = "text-align: left;">latitude</th><th style = "text-align: left;">floor_area</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String7" style = "text-align: left;">String7</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Float64" style = "text-align: left;">Float64</th><th title = "Float64" style = "text-align: left;">Float64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">S14046</td><td style = "text-align: left;">北山田店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市都筑区北山田一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシツヅキクキタヤマタイッチョウメ</td><td style = "text-align: left;">045-123-4049</td><td style = "text-align: right;">139.592</td><td style = "text-align: right;">35.5619</td><td style = "text-align: right;">831.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">S14011</td><td style = "text-align: left;">日吉本町店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市港北区日吉本町四丁目</td><td style = "text-align: left;">カナガワケンヨコハマシコウホククヒヨシホンチョウヨンチョウメ</td><td style = "text-align: left;">045-123-4033</td><td style = "text-align: right;">139.632</td><td style = "text-align: right;">35.5466</td><td style = "text-align: right;">890.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">S12013</td><td style = "text-align: left;">習志野店</td><td style = "text-align: left;">12</td><td style = "text-align: left;">千葉県</td><td style = "text-align: left;">千葉県習志野市芝園一丁目</td><td style = "text-align: left;">チバケンナラシノシシバゾノイッチョウメ</td><td style = "text-align: left;">047-123-4002</td><td style = "text-align: right;">140.022</td><td style = "text-align: right;">35.6612</td><td style = "text-align: right;">808.0</td></tr></tbody></table></div>

```julia
# 別解
df_store |>
    @filter(_.prefecture_cd != "13" && _.floor_area <= 900) |>
    DataFrame
```

<div><div style = "float: left;"><span>3×10 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">store_name</th><th style = "text-align: left;">prefecture_cd</th><th style = "text-align: left;">prefecture</th><th style = "text-align: left;">address</th><th style = "text-align: left;">address_kana</th><th style = "text-align: left;">tel_no</th><th style = "text-align: left;">longitude</th><th style = "text-align: left;">latitude</th><th style = "text-align: left;">floor_area</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String7" style = "text-align: left;">String7</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Float64" style = "text-align: left;">Float64</th><th title = "Float64" style = "text-align: left;">Float64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">S14046</td><td style = "text-align: left;">北山田店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市都筑区北山田一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシツヅキクキタヤマタイッチョウメ</td><td style = "text-align: left;">045-123-4049</td><td style = "text-align: right;">139.592</td><td style = "text-align: right;">35.5619</td><td style = "text-align: right;">831.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">S14011</td><td style = "text-align: left;">日吉本町店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市港北区日吉本町四丁目</td><td style = "text-align: left;">カナガワケンヨコハマシコウホククヒヨシホンチョウヨンチョウメ</td><td style = "text-align: left;">045-123-4033</td><td style = "text-align: right;">139.632</td><td style = "text-align: right;">35.5466</td><td style = "text-align: right;">890.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">S12013</td><td style = "text-align: left;">習志野店</td><td style = "text-align: left;">12</td><td style = "text-align: left;">千葉県</td><td style = "text-align: left;">千葉県習志野市芝園一丁目</td><td style = "text-align: left;">チバケンナラシノシシバゾノイッチョウメ</td><td style = "text-align: left;">047-123-4002</td><td style = "text-align: right;">140.022</td><td style = "text-align: right;">35.6612</td><td style = "text-align: right;">808.0</td></tr></tbody></table></div>

## 4.　部分文字列の有無による選択（正規表現）

* **正規表現に一致する行を選択することができる（これも論理式なので）。**
    * **Julia では `occursin()` を使う。**

---
> J-010: 店舗データ（df_store）から、店舗コード（store_cd）が"S14"で始まるものだけ全項目抽出し、10件表示せよ。

```julia
df = df_store[occursin.(r"^S14", df_store.store_cd), :]  # occursin のあとに . が必要
first(df, 10)
```

<div><div style = "float: left;"><span>10×10 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">store_name</th><th style = "text-align: left;">prefecture_cd</th><th style = "text-align: left;">prefecture</th><th style = "text-align: left;">address</th><th style = "text-align: left;">address_kana</th><th style = "text-align: left;">tel_no</th><th style = "text-align: left;">longitude</th><th style = "text-align: left;">latitude</th><th style = "text-align: left;">floor_area</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String7" style = "text-align: left;">String7</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Float64" style = "text-align: left;">Float64</th><th title = "Float64" style = "text-align: left;">Float64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">S14010</td><td style = "text-align: left;">菊名店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市港北区菊名一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシコウホククキクナイッチョウメ</td><td style = "text-align: left;">045-123-4032</td><td style = "text-align: right;">139.633</td><td style = "text-align: right;">35.5005</td><td style = "text-align: right;">1732.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">S14033</td><td style = "text-align: left;">阿久和店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市瀬谷区阿久和西一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシセヤクアクワニシイッチョウメ</td><td style = "text-align: left;">045-123-4043</td><td style = "text-align: right;">139.496</td><td style = "text-align: right;">35.4592</td><td style = "text-align: right;">1495.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">S14036</td><td style = "text-align: left;">相模原中央店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県相模原市中央二丁目</td><td style = "text-align: left;">カナガワケンサガミハラシチュウオウニチョウメ</td><td style = "text-align: left;">042-123-4045</td><td style = "text-align: right;">139.372</td><td style = "text-align: right;">35.5733</td><td style = "text-align: right;">1679.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">S14040</td><td style = "text-align: left;">長津田店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市緑区長津田みなみ台五丁目</td><td style = "text-align: left;">カナガワケンヨコハマシミドリクナガツタミナミダイゴチョウメ</td><td style = "text-align: left;">045-123-4046</td><td style = "text-align: right;">139.499</td><td style = "text-align: right;">35.524</td><td style = "text-align: right;">1548.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">S14050</td><td style = "text-align: left;">阿久和西店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市瀬谷区阿久和西一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシセヤクアクワニシイッチョウメ</td><td style = "text-align: left;">045-123-4053</td><td style = "text-align: right;">139.496</td><td style = "text-align: right;">35.4592</td><td style = "text-align: right;">1830.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">S14028</td><td style = "text-align: left;">二ツ橋店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市瀬谷区二ツ橋町</td><td style = "text-align: left;">カナガワケンヨコハマシセヤクフタツバシチョウ</td><td style = "text-align: left;">045-123-4042</td><td style = "text-align: right;">139.496</td><td style = "text-align: right;">35.463</td><td style = "text-align: right;">1574.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">S14012</td><td style = "text-align: left;">本牧和田店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市中区本牧和田</td><td style = "text-align: left;">カナガワケンヨコハマシナカクホンモクワダ</td><td style = "text-align: left;">045-123-4034</td><td style = "text-align: right;">139.658</td><td style = "text-align: right;">35.4216</td><td style = "text-align: right;">1341.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">S14046</td><td style = "text-align: left;">北山田店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市都筑区北山田一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシツヅキクキタヤマタイッチョウメ</td><td style = "text-align: left;">045-123-4049</td><td style = "text-align: right;">139.592</td><td style = "text-align: right;">35.5619</td><td style = "text-align: right;">831.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">S14022</td><td style = "text-align: left;">逗子店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県逗子市逗子一丁目</td><td style = "text-align: left;">カナガワケンズシシズシイッチョウメ</td><td style = "text-align: left;">046-123-4036</td><td style = "text-align: right;">139.579</td><td style = "text-align: right;">35.2964</td><td style = "text-align: right;">1838.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">S14011</td><td style = "text-align: left;">日吉本町店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市港北区日吉本町四丁目</td><td style = "text-align: left;">カナガワケンヨコハマシコウホククヒヨシホンチョウヨンチョウメ</td><td style = "text-align: left;">045-123-4033</td><td style = "text-align: right;">139.632</td><td style = "text-align: right;">35.5466</td><td style = "text-align: right;">890.0</td></tr></tbody></table></div>

```julia
# 別解
df_store |>
    @filter(occursin(r"^S14", _.store_cd)) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×10 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">store_name</th><th style = "text-align: left;">prefecture_cd</th><th style = "text-align: left;">prefecture</th><th style = "text-align: left;">address</th><th style = "text-align: left;">address_kana</th><th style = "text-align: left;">tel_no</th><th style = "text-align: left;">longitude</th><th style = "text-align: left;">latitude</th><th style = "text-align: left;">floor_area</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String7" style = "text-align: left;">String7</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Float64" style = "text-align: left;">Float64</th><th title = "Float64" style = "text-align: left;">Float64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">S14010</td><td style = "text-align: left;">菊名店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市港北区菊名一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシコウホククキクナイッチョウメ</td><td style = "text-align: left;">045-123-4032</td><td style = "text-align: right;">139.633</td><td style = "text-align: right;">35.5005</td><td style = "text-align: right;">1732.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">S14033</td><td style = "text-align: left;">阿久和店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市瀬谷区阿久和西一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシセヤクアクワニシイッチョウメ</td><td style = "text-align: left;">045-123-4043</td><td style = "text-align: right;">139.496</td><td style = "text-align: right;">35.4592</td><td style = "text-align: right;">1495.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">S14036</td><td style = "text-align: left;">相模原中央店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県相模原市中央二丁目</td><td style = "text-align: left;">カナガワケンサガミハラシチュウオウニチョウメ</td><td style = "text-align: left;">042-123-4045</td><td style = "text-align: right;">139.372</td><td style = "text-align: right;">35.5733</td><td style = "text-align: right;">1679.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">S14040</td><td style = "text-align: left;">長津田店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市緑区長津田みなみ台五丁目</td><td style = "text-align: left;">カナガワケンヨコハマシミドリクナガツタミナミダイゴチョウメ</td><td style = "text-align: left;">045-123-4046</td><td style = "text-align: right;">139.499</td><td style = "text-align: right;">35.524</td><td style = "text-align: right;">1548.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">S14050</td><td style = "text-align: left;">阿久和西店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市瀬谷区阿久和西一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシセヤクアクワニシイッチョウメ</td><td style = "text-align: left;">045-123-4053</td><td style = "text-align: right;">139.496</td><td style = "text-align: right;">35.4592</td><td style = "text-align: right;">1830.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">S14028</td><td style = "text-align: left;">二ツ橋店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市瀬谷区二ツ橋町</td><td style = "text-align: left;">カナガワケンヨコハマシセヤクフタツバシチョウ</td><td style = "text-align: left;">045-123-4042</td><td style = "text-align: right;">139.496</td><td style = "text-align: right;">35.463</td><td style = "text-align: right;">1574.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">S14012</td><td style = "text-align: left;">本牧和田店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市中区本牧和田</td><td style = "text-align: left;">カナガワケンヨコハマシナカクホンモクワダ</td><td style = "text-align: left;">045-123-4034</td><td style = "text-align: right;">139.658</td><td style = "text-align: right;">35.4216</td><td style = "text-align: right;">1341.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">S14046</td><td style = "text-align: left;">北山田店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市都筑区北山田一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシツヅキクキタヤマタイッチョウメ</td><td style = "text-align: left;">045-123-4049</td><td style = "text-align: right;">139.592</td><td style = "text-align: right;">35.5619</td><td style = "text-align: right;">831.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">S14022</td><td style = "text-align: left;">逗子店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県逗子市逗子一丁目</td><td style = "text-align: left;">カナガワケンズシシズシイッチョウメ</td><td style = "text-align: left;">046-123-4036</td><td style = "text-align: right;">139.579</td><td style = "text-align: right;">35.2964</td><td style = "text-align: right;">1838.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">S14011</td><td style = "text-align: left;">日吉本町店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市港北区日吉本町四丁目</td><td style = "text-align: left;">カナガワケンヨコハマシコウホククヒヨシホンチョウヨンチョウメ</td><td style = "text-align: left;">045-123-4033</td><td style = "text-align: right;">139.632</td><td style = "text-align: right;">35.5466</td><td style = "text-align: right;">890.0</td></tr></tbody></table></div>

---
> J-011: 顧客データ（df_customer）から顧客ID（customer_id）の末尾が1のものだけ全項目抽出し、10件表示せよ。

```julia
df = df_customer[occursin.(r"1$", df_customer.customer_id), :]
first(df, 10)
```

<div><div style = "float: left;"><span>10×11 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">customer_name</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">gender</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">postal_cd</th><th style = "text-align: left;">address</th><th style = "text-align: left;">application_store_cd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">status_cd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS037613000071</td><td style = "text-align: left;">六角 雅彦</td><td style = "text-align: left;">9</td><td style = "text-align: left;">不明</td><td style = "text-align: left;">1952-04-01</td><td style = "text-align: right;">66</td><td style = "text-align: left;">136-0076</td><td style = "text-align: left;">東京都江東区南砂**********</td><td style = "text-align: left;">S13037</td><td style = "text-align: left;">20150414</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS028811000001</td><td style = "text-align: left;">堀井 かおり</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1933-03-27</td><td style = "text-align: right;">86</td><td style = "text-align: left;">245-0016</td><td style = "text-align: left;">神奈川県横浜市泉区和泉町**********</td><td style = "text-align: left;">S14028</td><td style = "text-align: left;">20160115</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS040412000191</td><td style = "text-align: left;">川井 郁恵</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1977-01-05</td><td style = "text-align: right;">42</td><td style = "text-align: left;">226-0021</td><td style = "text-align: left;">神奈川県横浜市緑区北八朔町**********</td><td style = "text-align: left;">S14040</td><td style = "text-align: left;">20151101</td><td style = "text-align: left;">1-20091025-4</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS028314000011</td><td style = "text-align: left;">小菅 あおい</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1983-11-26</td><td style = "text-align: right;">35</td><td style = "text-align: left;">246-0038</td><td style = "text-align: left;">神奈川県横浜市瀬谷区宮沢**********</td><td style = "text-align: left;">S14028</td><td style = "text-align: left;">20151123</td><td style = "text-align: left;">1-20080426-5</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS039212000051</td><td style = "text-align: left;">藤島 恵梨香</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1997-02-03</td><td style = "text-align: right;">22</td><td style = "text-align: left;">166-0001</td><td style = "text-align: left;">東京都杉並区阿佐谷北**********</td><td style = "text-align: left;">S13039</td><td style = "text-align: left;">20171121</td><td style = "text-align: left;">1-20100215-4</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS015412000111</td><td style = "text-align: left;">松居 奈月</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1972-10-04</td><td style = "text-align: right;">46</td><td style = "text-align: left;">136-0071</td><td style = "text-align: left;">東京都江東区亀戸**********</td><td style = "text-align: left;">S13015</td><td style = "text-align: left;">20150629</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS004702000041</td><td style = "text-align: left;">野島 洋</td><td style = "text-align: left;">0</td><td style = "text-align: left;">男性</td><td style = "text-align: left;">1943-08-24</td><td style = "text-align: right;">75</td><td style = "text-align: left;">176-0022</td><td style = "text-align: left;">東京都練馬区向山**********</td><td style = "text-align: left;">S13004</td><td style = "text-align: left;">20170218</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS041515000001</td><td style = "text-align: left;">栗田 千夏</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1967-01-02</td><td style = "text-align: right;">52</td><td style = "text-align: left;">206-0001</td><td style = "text-align: left;">東京都多摩市和田**********</td><td style = "text-align: left;">S13041</td><td style = "text-align: left;">20160422</td><td style = "text-align: left;">E-20100803-F</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS029313000221</td><td style = "text-align: left;">北条 ひかり</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1987-06-19</td><td style = "text-align: right;">31</td><td style = "text-align: left;">279-0011</td><td style = "text-align: left;">千葉県浦安市美浜**********</td><td style = "text-align: left;">S12029</td><td style = "text-align: left;">20180810</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS034312000071</td><td style = "text-align: left;">望月 奈央</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1980-09-20</td><td style = "text-align: right;">38</td><td style = "text-align: left;">213-0026</td><td style = "text-align: left;">神奈川県川崎市高津区久末**********</td><td style = "text-align: left;">S14034</td><td style = "text-align: left;">20160106</td><td style = "text-align: left;">0-00000000-0</td></tr></tbody></table></div>

```julia
# 別解2
df_customer |>
    @filter(occursin(r"1$", _.customer_id)) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×11 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">customer_name</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">gender</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">postal_cd</th><th style = "text-align: left;">address</th><th style = "text-align: left;">application_store_cd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">status_cd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS037613000071</td><td style = "text-align: left;">六角 雅彦</td><td style = "text-align: left;">9</td><td style = "text-align: left;">不明</td><td style = "text-align: left;">1952-04-01</td><td style = "text-align: right;">66</td><td style = "text-align: left;">136-0076</td><td style = "text-align: left;">東京都江東区南砂**********</td><td style = "text-align: left;">S13037</td><td style = "text-align: left;">20150414</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS028811000001</td><td style = "text-align: left;">堀井 かおり</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1933-03-27</td><td style = "text-align: right;">86</td><td style = "text-align: left;">245-0016</td><td style = "text-align: left;">神奈川県横浜市泉区和泉町**********</td><td style = "text-align: left;">S14028</td><td style = "text-align: left;">20160115</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS040412000191</td><td style = "text-align: left;">川井 郁恵</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1977-01-05</td><td style = "text-align: right;">42</td><td style = "text-align: left;">226-0021</td><td style = "text-align: left;">神奈川県横浜市緑区北八朔町**********</td><td style = "text-align: left;">S14040</td><td style = "text-align: left;">20151101</td><td style = "text-align: left;">1-20091025-4</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS028314000011</td><td style = "text-align: left;">小菅 あおい</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1983-11-26</td><td style = "text-align: right;">35</td><td style = "text-align: left;">246-0038</td><td style = "text-align: left;">神奈川県横浜市瀬谷区宮沢**********</td><td style = "text-align: left;">S14028</td><td style = "text-align: left;">20151123</td><td style = "text-align: left;">1-20080426-5</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS039212000051</td><td style = "text-align: left;">藤島 恵梨香</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1997-02-03</td><td style = "text-align: right;">22</td><td style = "text-align: left;">166-0001</td><td style = "text-align: left;">東京都杉並区阿佐谷北**********</td><td style = "text-align: left;">S13039</td><td style = "text-align: left;">20171121</td><td style = "text-align: left;">1-20100215-4</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS015412000111</td><td style = "text-align: left;">松居 奈月</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1972-10-04</td><td style = "text-align: right;">46</td><td style = "text-align: left;">136-0071</td><td style = "text-align: left;">東京都江東区亀戸**********</td><td style = "text-align: left;">S13015</td><td style = "text-align: left;">20150629</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS004702000041</td><td style = "text-align: left;">野島 洋</td><td style = "text-align: left;">0</td><td style = "text-align: left;">男性</td><td style = "text-align: left;">1943-08-24</td><td style = "text-align: right;">75</td><td style = "text-align: left;">176-0022</td><td style = "text-align: left;">東京都練馬区向山**********</td><td style = "text-align: left;">S13004</td><td style = "text-align: left;">20170218</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS041515000001</td><td style = "text-align: left;">栗田 千夏</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1967-01-02</td><td style = "text-align: right;">52</td><td style = "text-align: left;">206-0001</td><td style = "text-align: left;">東京都多摩市和田**********</td><td style = "text-align: left;">S13041</td><td style = "text-align: left;">20160422</td><td style = "text-align: left;">E-20100803-F</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS029313000221</td><td style = "text-align: left;">北条 ひかり</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1987-06-19</td><td style = "text-align: right;">31</td><td style = "text-align: left;">279-0011</td><td style = "text-align: left;">千葉県浦安市美浜**********</td><td style = "text-align: left;">S12029</td><td style = "text-align: left;">20180810</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS034312000071</td><td style = "text-align: left;">望月 奈央</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1980-09-20</td><td style = "text-align: right;">38</td><td style = "text-align: left;">213-0026</td><td style = "text-align: left;">神奈川県川崎市高津区久末**********</td><td style = "text-align: left;">S14034</td><td style = "text-align: left;">20160106</td><td style = "text-align: left;">0-00000000-0</td></tr></tbody></table></div>

---
> J-012: 店舗データ（df_store）から、住所 (address) に"横浜市"が含まれるものだけ全項目表示せよ。

```julia
df = df_store[occursin.(r"横浜市", df_store.address), :]
df
```

<div><div style = "float: left;"><span>11×10 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">store_name</th><th style = "text-align: left;">prefecture_cd</th><th style = "text-align: left;">prefecture</th><th style = "text-align: left;">address</th><th style = "text-align: left;">address_kana</th><th style = "text-align: left;">tel_no</th><th style = "text-align: left;">longitude</th><th style = "text-align: left;">latitude</th><th style = "text-align: left;">floor_area</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String7" style = "text-align: left;">String7</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Float64" style = "text-align: left;">Float64</th><th title = "Float64" style = "text-align: left;">Float64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">S14010</td><td style = "text-align: left;">菊名店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市港北区菊名一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシコウホククキクナイッチョウメ</td><td style = "text-align: left;">045-123-4032</td><td style = "text-align: right;">139.633</td><td style = "text-align: right;">35.5005</td><td style = "text-align: right;">1732.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">S14033</td><td style = "text-align: left;">阿久和店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市瀬谷区阿久和西一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシセヤクアクワニシイッチョウメ</td><td style = "text-align: left;">045-123-4043</td><td style = "text-align: right;">139.496</td><td style = "text-align: right;">35.4592</td><td style = "text-align: right;">1495.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">S14040</td><td style = "text-align: left;">長津田店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市緑区長津田みなみ台五丁目</td><td style = "text-align: left;">カナガワケンヨコハマシミドリクナガツタミナミダイゴチョウメ</td><td style = "text-align: left;">045-123-4046</td><td style = "text-align: right;">139.499</td><td style = "text-align: right;">35.524</td><td style = "text-align: right;">1548.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">S14050</td><td style = "text-align: left;">阿久和西店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市瀬谷区阿久和西一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシセヤクアクワニシイッチョウメ</td><td style = "text-align: left;">045-123-4053</td><td style = "text-align: right;">139.496</td><td style = "text-align: right;">35.4592</td><td style = "text-align: right;">1830.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">S14028</td><td style = "text-align: left;">二ツ橋店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市瀬谷区二ツ橋町</td><td style = "text-align: left;">カナガワケンヨコハマシセヤクフタツバシチョウ</td><td style = "text-align: left;">045-123-4042</td><td style = "text-align: right;">139.496</td><td style = "text-align: right;">35.463</td><td style = "text-align: right;">1574.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">S14012</td><td style = "text-align: left;">本牧和田店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市中区本牧和田</td><td style = "text-align: left;">カナガワケンヨコハマシナカクホンモクワダ</td><td style = "text-align: left;">045-123-4034</td><td style = "text-align: right;">139.658</td><td style = "text-align: right;">35.4216</td><td style = "text-align: right;">1341.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">S14046</td><td style = "text-align: left;">北山田店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市都筑区北山田一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシツヅキクキタヤマタイッチョウメ</td><td style = "text-align: left;">045-123-4049</td><td style = "text-align: right;">139.592</td><td style = "text-align: right;">35.5619</td><td style = "text-align: right;">831.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">S14011</td><td style = "text-align: left;">日吉本町店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市港北区日吉本町四丁目</td><td style = "text-align: left;">カナガワケンヨコハマシコウホククヒヨシホンチョウヨンチョウメ</td><td style = "text-align: left;">045-123-4033</td><td style = "text-align: right;">139.632</td><td style = "text-align: right;">35.5466</td><td style = "text-align: right;">890.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">S14048</td><td style = "text-align: left;">中川中央店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市都筑区中川中央二丁目</td><td style = "text-align: left;">カナガワケンヨコハマシツヅキクナカガワチュウオウニチョウメ</td><td style = "text-align: left;">045-123-4051</td><td style = "text-align: right;">139.576</td><td style = "text-align: right;">35.5491</td><td style = "text-align: right;">1657.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">S14042</td><td style = "text-align: left;">新山下店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市中区新山下二丁目</td><td style = "text-align: left;">カナガワケンヨコハマシナカクシンヤマシタニチョウメ</td><td style = "text-align: left;">045-123-4047</td><td style = "text-align: right;">139.659</td><td style = "text-align: right;">35.4389</td><td style = "text-align: right;">1044.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">11</td><td style = "text-align: left;">S14006</td><td style = "text-align: left;">葛が谷店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市都筑区葛が谷</td><td style = "text-align: left;">カナガワケンヨコハマシツヅキククズガヤ</td><td style = "text-align: left;">045-123-4031</td><td style = "text-align: right;">139.563</td><td style = "text-align: right;">35.5357</td><td style = "text-align: right;">1886.0</td></tr></tbody></table></div>

```julia
# 別解
df_store |>
    @filter(occursin(r"横浜市", _.address)) |>
    DataFrame
```

<div><div style = "float: left;"><span>11×10 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">store_name</th><th style = "text-align: left;">prefecture_cd</th><th style = "text-align: left;">prefecture</th><th style = "text-align: left;">address</th><th style = "text-align: left;">address_kana</th><th style = "text-align: left;">tel_no</th><th style = "text-align: left;">longitude</th><th style = "text-align: left;">latitude</th><th style = "text-align: left;">floor_area</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String7" style = "text-align: left;">String7</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Float64" style = "text-align: left;">Float64</th><th title = "Float64" style = "text-align: left;">Float64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">S14010</td><td style = "text-align: left;">菊名店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市港北区菊名一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシコウホククキクナイッチョウメ</td><td style = "text-align: left;">045-123-4032</td><td style = "text-align: right;">139.633</td><td style = "text-align: right;">35.5005</td><td style = "text-align: right;">1732.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">S14033</td><td style = "text-align: left;">阿久和店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市瀬谷区阿久和西一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシセヤクアクワニシイッチョウメ</td><td style = "text-align: left;">045-123-4043</td><td style = "text-align: right;">139.496</td><td style = "text-align: right;">35.4592</td><td style = "text-align: right;">1495.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">S14040</td><td style = "text-align: left;">長津田店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市緑区長津田みなみ台五丁目</td><td style = "text-align: left;">カナガワケンヨコハマシミドリクナガツタミナミダイゴチョウメ</td><td style = "text-align: left;">045-123-4046</td><td style = "text-align: right;">139.499</td><td style = "text-align: right;">35.524</td><td style = "text-align: right;">1548.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">S14050</td><td style = "text-align: left;">阿久和西店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市瀬谷区阿久和西一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシセヤクアクワニシイッチョウメ</td><td style = "text-align: left;">045-123-4053</td><td style = "text-align: right;">139.496</td><td style = "text-align: right;">35.4592</td><td style = "text-align: right;">1830.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">S14028</td><td style = "text-align: left;">二ツ橋店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市瀬谷区二ツ橋町</td><td style = "text-align: left;">カナガワケンヨコハマシセヤクフタツバシチョウ</td><td style = "text-align: left;">045-123-4042</td><td style = "text-align: right;">139.496</td><td style = "text-align: right;">35.463</td><td style = "text-align: right;">1574.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">S14012</td><td style = "text-align: left;">本牧和田店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市中区本牧和田</td><td style = "text-align: left;">カナガワケンヨコハマシナカクホンモクワダ</td><td style = "text-align: left;">045-123-4034</td><td style = "text-align: right;">139.658</td><td style = "text-align: right;">35.4216</td><td style = "text-align: right;">1341.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">S14046</td><td style = "text-align: left;">北山田店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市都筑区北山田一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシツヅキクキタヤマタイッチョウメ</td><td style = "text-align: left;">045-123-4049</td><td style = "text-align: right;">139.592</td><td style = "text-align: right;">35.5619</td><td style = "text-align: right;">831.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">S14011</td><td style = "text-align: left;">日吉本町店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市港北区日吉本町四丁目</td><td style = "text-align: left;">カナガワケンヨコハマシコウホククヒヨシホンチョウヨンチョウメ</td><td style = "text-align: left;">045-123-4033</td><td style = "text-align: right;">139.632</td><td style = "text-align: right;">35.5466</td><td style = "text-align: right;">890.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">S14048</td><td style = "text-align: left;">中川中央店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市都筑区中川中央二丁目</td><td style = "text-align: left;">カナガワケンヨコハマシツヅキクナカガワチュウオウニチョウメ</td><td style = "text-align: left;">045-123-4051</td><td style = "text-align: right;">139.576</td><td style = "text-align: right;">35.5491</td><td style = "text-align: right;">1657.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">S14042</td><td style = "text-align: left;">新山下店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市中区新山下二丁目</td><td style = "text-align: left;">カナガワケンヨコハマシナカクシンヤマシタニチョウメ</td><td style = "text-align: left;">045-123-4047</td><td style = "text-align: right;">139.659</td><td style = "text-align: right;">35.4389</td><td style = "text-align: right;">1044.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">11</td><td style = "text-align: left;">S14006</td><td style = "text-align: left;">葛が谷店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市都筑区葛が谷</td><td style = "text-align: left;">カナガワケンヨコハマシツヅキククズガヤ</td><td style = "text-align: left;">045-123-4031</td><td style = "text-align: right;">139.563</td><td style = "text-align: right;">35.5357</td><td style = "text-align: right;">1886.0</td></tr></tbody></table></div>

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

```julia
df = df_customer[occursin.(r"^[A-F]", df_customer.status_cd), :]
first(df, 10)
```

<div><div style = "float: left;"><span>10×11 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">customer_name</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">gender</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">postal_cd</th><th style = "text-align: left;">address</th><th style = "text-align: left;">application_store_cd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">status_cd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS031415000172</td><td style = "text-align: left;">宇多田 貴美子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1976-10-04</td><td style = "text-align: right;">42</td><td style = "text-align: left;">151-0053</td><td style = "text-align: left;">東京都渋谷区代々木**********</td><td style = "text-align: left;">S13031</td><td style = "text-align: left;">20150529</td><td style = "text-align: left;">D-20100325-C</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS015414000103</td><td style = "text-align: left;">奥野 陽子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1977-08-09</td><td style = "text-align: right;">41</td><td style = "text-align: left;">136-0073</td><td style = "text-align: left;">東京都江東区北砂**********</td><td style = "text-align: left;">S13015</td><td style = "text-align: left;">20150722</td><td style = "text-align: left;">B-20100609-B</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS011215000048</td><td style = "text-align: left;">芦田 沙耶</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1992-02-01</td><td style = "text-align: right;">27</td><td style = "text-align: left;">223-0062</td><td style = "text-align: left;">神奈川県横浜市港北区日吉本町**********</td><td style = "text-align: left;">S14011</td><td style = "text-align: left;">20150228</td><td style = "text-align: left;">C-20100421-9</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS029415000023</td><td style = "text-align: left;">梅田 里穂</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1976-01-17</td><td style = "text-align: right;">43</td><td style = "text-align: left;">279-0043</td><td style = "text-align: left;">千葉県浦安市富士見**********</td><td style = "text-align: left;">S12029</td><td style = "text-align: left;">20150610</td><td style = "text-align: left;">D-20100918-E</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS035415000029</td><td style = "text-align: left;">寺沢 真希</td><td style = "text-align: left;">9</td><td style = "text-align: left;">不明</td><td style = "text-align: left;">1977-09-27</td><td style = "text-align: right;">41</td><td style = "text-align: left;">158-0096</td><td style = "text-align: left;">東京都世田谷区玉川台**********</td><td style = "text-align: left;">S13035</td><td style = "text-align: left;">20141220</td><td style = "text-align: left;">F-20101029-F</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS031415000106</td><td style = "text-align: left;">宇野 由美子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1970-02-26</td><td style = "text-align: right;">49</td><td style = "text-align: left;">151-0053</td><td style = "text-align: left;">東京都渋谷区代々木**********</td><td style = "text-align: left;">S13031</td><td style = "text-align: left;">20150201</td><td style = "text-align: left;">F-20100511-E</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS029215000025</td><td style = "text-align: left;">石倉 美帆</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1993-09-28</td><td style = "text-align: right;">25</td><td style = "text-align: left;">279-0022</td><td style = "text-align: left;">千葉県浦安市今川**********</td><td style = "text-align: left;">S12029</td><td style = "text-align: left;">20150708</td><td style = "text-align: left;">B-20100820-C</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS033605000005</td><td style = "text-align: left;">猪股 雄太</td><td style = "text-align: left;">0</td><td style = "text-align: left;">男性</td><td style = "text-align: left;">1955-12-05</td><td style = "text-align: right;">63</td><td style = "text-align: left;">246-0031</td><td style = "text-align: left;">神奈川県横浜市瀬谷区瀬谷**********</td><td style = "text-align: left;">S14033</td><td style = "text-align: left;">20150425</td><td style = "text-align: left;">F-20100917-E</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS033415000229</td><td style = "text-align: left;">板垣 菜々美</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1977-11-07</td><td style = "text-align: right;">41</td><td style = "text-align: left;">246-0021</td><td style = "text-align: left;">神奈川県横浜市瀬谷区二ツ橋町**********</td><td style = "text-align: left;">S14033</td><td style = "text-align: left;">20150712</td><td style = "text-align: left;">F-20100326-E</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS008415000145</td><td style = "text-align: left;">黒谷 麻緒</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1977-06-27</td><td style = "text-align: right;">41</td><td style = "text-align: left;">157-0067</td><td style = "text-align: left;">東京都世田谷区喜多見**********</td><td style = "text-align: left;">S13008</td><td style = "text-align: left;">20150829</td><td style = "text-align: left;">F-20100622-F</td></tr></tbody></table></div>

```julia
# 別解
df_customer |>
    @filter(occursin(r"^[A-F]", _.status_cd)) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×11 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">customer_name</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">gender</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">postal_cd</th><th style = "text-align: left;">address</th><th style = "text-align: left;">application_store_cd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">status_cd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS031415000172</td><td style = "text-align: left;">宇多田 貴美子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1976-10-04</td><td style = "text-align: right;">42</td><td style = "text-align: left;">151-0053</td><td style = "text-align: left;">東京都渋谷区代々木**********</td><td style = "text-align: left;">S13031</td><td style = "text-align: left;">20150529</td><td style = "text-align: left;">D-20100325-C</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS015414000103</td><td style = "text-align: left;">奥野 陽子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1977-08-09</td><td style = "text-align: right;">41</td><td style = "text-align: left;">136-0073</td><td style = "text-align: left;">東京都江東区北砂**********</td><td style = "text-align: left;">S13015</td><td style = "text-align: left;">20150722</td><td style = "text-align: left;">B-20100609-B</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS011215000048</td><td style = "text-align: left;">芦田 沙耶</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1992-02-01</td><td style = "text-align: right;">27</td><td style = "text-align: left;">223-0062</td><td style = "text-align: left;">神奈川県横浜市港北区日吉本町**********</td><td style = "text-align: left;">S14011</td><td style = "text-align: left;">20150228</td><td style = "text-align: left;">C-20100421-9</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS029415000023</td><td style = "text-align: left;">梅田 里穂</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1976-01-17</td><td style = "text-align: right;">43</td><td style = "text-align: left;">279-0043</td><td style = "text-align: left;">千葉県浦安市富士見**********</td><td style = "text-align: left;">S12029</td><td style = "text-align: left;">20150610</td><td style = "text-align: left;">D-20100918-E</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS035415000029</td><td style = "text-align: left;">寺沢 真希</td><td style = "text-align: left;">9</td><td style = "text-align: left;">不明</td><td style = "text-align: left;">1977-09-27</td><td style = "text-align: right;">41</td><td style = "text-align: left;">158-0096</td><td style = "text-align: left;">東京都世田谷区玉川台**********</td><td style = "text-align: left;">S13035</td><td style = "text-align: left;">20141220</td><td style = "text-align: left;">F-20101029-F</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS031415000106</td><td style = "text-align: left;">宇野 由美子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1970-02-26</td><td style = "text-align: right;">49</td><td style = "text-align: left;">151-0053</td><td style = "text-align: left;">東京都渋谷区代々木**********</td><td style = "text-align: left;">S13031</td><td style = "text-align: left;">20150201</td><td style = "text-align: left;">F-20100511-E</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS029215000025</td><td style = "text-align: left;">石倉 美帆</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1993-09-28</td><td style = "text-align: right;">25</td><td style = "text-align: left;">279-0022</td><td style = "text-align: left;">千葉県浦安市今川**********</td><td style = "text-align: left;">S12029</td><td style = "text-align: left;">20150708</td><td style = "text-align: left;">B-20100820-C</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS033605000005</td><td style = "text-align: left;">猪股 雄太</td><td style = "text-align: left;">0</td><td style = "text-align: left;">男性</td><td style = "text-align: left;">1955-12-05</td><td style = "text-align: right;">63</td><td style = "text-align: left;">246-0031</td><td style = "text-align: left;">神奈川県横浜市瀬谷区瀬谷**********</td><td style = "text-align: left;">S14033</td><td style = "text-align: left;">20150425</td><td style = "text-align: left;">F-20100917-E</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS033415000229</td><td style = "text-align: left;">板垣 菜々美</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1977-11-07</td><td style = "text-align: right;">41</td><td style = "text-align: left;">246-0021</td><td style = "text-align: left;">神奈川県横浜市瀬谷区二ツ橋町**********</td><td style = "text-align: left;">S14033</td><td style = "text-align: left;">20150712</td><td style = "text-align: left;">F-20100326-E</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS008415000145</td><td style = "text-align: left;">黒谷 麻緒</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1977-06-27</td><td style = "text-align: right;">41</td><td style = "text-align: left;">157-0067</td><td style = "text-align: left;">東京都世田谷区喜多見**********</td><td style = "text-align: left;">S13008</td><td style = "text-align: left;">20150829</td><td style = "text-align: left;">F-20100622-F</td></tr></tbody></table></div>

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

```julia
df = df_customer[occursin.(r"[1-9]$", df_customer.status_cd), :]
first(df, 10)
```

<div><div style = "float: left;"><span>10×11 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">customer_name</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">gender</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">postal_cd</th><th style = "text-align: left;">address</th><th style = "text-align: left;">application_store_cd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">status_cd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001215000145</td><td style = "text-align: left;">田崎 美紀</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1995-03-29</td><td style = "text-align: right;">24</td><td style = "text-align: left;">144-0055</td><td style = "text-align: left;">東京都大田区仲六郷**********</td><td style = "text-align: left;">S13001</td><td style = "text-align: left;">20170605</td><td style = "text-align: left;">6-20090929-2</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS033513000180</td><td style = "text-align: left;">安斎 遥</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1962-07-11</td><td style = "text-align: right;">56</td><td style = "text-align: left;">241-0823</td><td style = "text-align: left;">神奈川県横浜市旭区善部町**********</td><td style = "text-align: left;">S14033</td><td style = "text-align: left;">20150728</td><td style = "text-align: left;">6-20080506-5</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS011215000048</td><td style = "text-align: left;">芦田 沙耶</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1992-02-01</td><td style = "text-align: right;">27</td><td style = "text-align: left;">223-0062</td><td style = "text-align: left;">神奈川県横浜市港北区日吉本町**********</td><td style = "text-align: left;">S14011</td><td style = "text-align: left;">20150228</td><td style = "text-align: left;">C-20100421-9</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS040412000191</td><td style = "text-align: left;">川井 郁恵</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1977-01-05</td><td style = "text-align: right;">42</td><td style = "text-align: left;">226-0021</td><td style = "text-align: left;">神奈川県横浜市緑区北八朔町**********</td><td style = "text-align: left;">S14040</td><td style = "text-align: left;">20151101</td><td style = "text-align: left;">1-20091025-4</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS009315000023</td><td style = "text-align: left;">皆川 文世</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1980-04-15</td><td style = "text-align: right;">38</td><td style = "text-align: left;">154-0012</td><td style = "text-align: left;">東京都世田谷区駒沢**********</td><td style = "text-align: left;">S13009</td><td style = "text-align: left;">20150319</td><td style = "text-align: left;">5-20080322-1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS015315000033</td><td style = "text-align: left;">福士 璃奈子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1983-03-17</td><td style = "text-align: right;">36</td><td style = "text-align: left;">135-0043</td><td style = "text-align: left;">東京都江東区塩浜**********</td><td style = "text-align: left;">S13015</td><td style = "text-align: left;">20141024</td><td style = "text-align: left;">4-20080219-3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS023513000066</td><td style = "text-align: left;">神戸 そら</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1961-12-17</td><td style = "text-align: right;">57</td><td style = "text-align: left;">210-0005</td><td style = "text-align: left;">神奈川県川崎市川崎区東田町**********</td><td style = "text-align: left;">S14023</td><td style = "text-align: left;">20150915</td><td style = "text-align: left;">5-20100524-9</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS035513000134</td><td style = "text-align: left;">市川 美帆</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1960-03-27</td><td style = "text-align: right;">59</td><td style = "text-align: left;">156-0053</td><td style = "text-align: left;">東京都世田谷区桜**********</td><td style = "text-align: left;">S13035</td><td style = "text-align: left;">20150227</td><td style = "text-align: left;">8-20100711-9</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001515000263</td><td style = "text-align: left;">高松 夏空</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1962-11-09</td><td style = "text-align: right;">56</td><td style = "text-align: left;">144-0051</td><td style = "text-align: left;">東京都大田区西蒲田**********</td><td style = "text-align: left;">S13001</td><td style = "text-align: left;">20160812</td><td style = "text-align: left;">1-20100804-1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS040314000027</td><td style = "text-align: left;">鶴田 きみまろ</td><td style = "text-align: left;">9</td><td style = "text-align: left;">不明</td><td style = "text-align: left;">1986-03-26</td><td style = "text-align: right;">33</td><td style = "text-align: left;">226-0027</td><td style = "text-align: left;">神奈川県横浜市緑区長津田**********</td><td style = "text-align: left;">S14040</td><td style = "text-align: left;">20150122</td><td style = "text-align: left;">2-20080426-4</td></tr></tbody></table></div>

```julia
# 別解
df_customer |>
    @filter(occursin(r"[1-9]$", _.status_cd)) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×11 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">customer_name</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">gender</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">postal_cd</th><th style = "text-align: left;">address</th><th style = "text-align: left;">application_store_cd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">status_cd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001215000145</td><td style = "text-align: left;">田崎 美紀</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1995-03-29</td><td style = "text-align: right;">24</td><td style = "text-align: left;">144-0055</td><td style = "text-align: left;">東京都大田区仲六郷**********</td><td style = "text-align: left;">S13001</td><td style = "text-align: left;">20170605</td><td style = "text-align: left;">6-20090929-2</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS033513000180</td><td style = "text-align: left;">安斎 遥</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1962-07-11</td><td style = "text-align: right;">56</td><td style = "text-align: left;">241-0823</td><td style = "text-align: left;">神奈川県横浜市旭区善部町**********</td><td style = "text-align: left;">S14033</td><td style = "text-align: left;">20150728</td><td style = "text-align: left;">6-20080506-5</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS011215000048</td><td style = "text-align: left;">芦田 沙耶</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1992-02-01</td><td style = "text-align: right;">27</td><td style = "text-align: left;">223-0062</td><td style = "text-align: left;">神奈川県横浜市港北区日吉本町**********</td><td style = "text-align: left;">S14011</td><td style = "text-align: left;">20150228</td><td style = "text-align: left;">C-20100421-9</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS040412000191</td><td style = "text-align: left;">川井 郁恵</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1977-01-05</td><td style = "text-align: right;">42</td><td style = "text-align: left;">226-0021</td><td style = "text-align: left;">神奈川県横浜市緑区北八朔町**********</td><td style = "text-align: left;">S14040</td><td style = "text-align: left;">20151101</td><td style = "text-align: left;">1-20091025-4</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS009315000023</td><td style = "text-align: left;">皆川 文世</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1980-04-15</td><td style = "text-align: right;">38</td><td style = "text-align: left;">154-0012</td><td style = "text-align: left;">東京都世田谷区駒沢**********</td><td style = "text-align: left;">S13009</td><td style = "text-align: left;">20150319</td><td style = "text-align: left;">5-20080322-1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS015315000033</td><td style = "text-align: left;">福士 璃奈子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1983-03-17</td><td style = "text-align: right;">36</td><td style = "text-align: left;">135-0043</td><td style = "text-align: left;">東京都江東区塩浜**********</td><td style = "text-align: left;">S13015</td><td style = "text-align: left;">20141024</td><td style = "text-align: left;">4-20080219-3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS023513000066</td><td style = "text-align: left;">神戸 そら</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1961-12-17</td><td style = "text-align: right;">57</td><td style = "text-align: left;">210-0005</td><td style = "text-align: left;">神奈川県川崎市川崎区東田町**********</td><td style = "text-align: left;">S14023</td><td style = "text-align: left;">20150915</td><td style = "text-align: left;">5-20100524-9</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS035513000134</td><td style = "text-align: left;">市川 美帆</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1960-03-27</td><td style = "text-align: right;">59</td><td style = "text-align: left;">156-0053</td><td style = "text-align: left;">東京都世田谷区桜**********</td><td style = "text-align: left;">S13035</td><td style = "text-align: left;">20150227</td><td style = "text-align: left;">8-20100711-9</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001515000263</td><td style = "text-align: left;">高松 夏空</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1962-11-09</td><td style = "text-align: right;">56</td><td style = "text-align: left;">144-0051</td><td style = "text-align: left;">東京都大田区西蒲田**********</td><td style = "text-align: left;">S13001</td><td style = "text-align: left;">20160812</td><td style = "text-align: left;">1-20100804-1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS040314000027</td><td style = "text-align: left;">鶴田 きみまろ</td><td style = "text-align: left;">9</td><td style = "text-align: left;">不明</td><td style = "text-align: left;">1986-03-26</td><td style = "text-align: right;">33</td><td style = "text-align: left;">226-0027</td><td style = "text-align: left;">神奈川県横浜市緑区長津田**********</td><td style = "text-align: left;">S14040</td><td style = "text-align: left;">20150122</td><td style = "text-align: left;">2-20080426-4</td></tr></tbody></table></div>

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

```julia
df = df_customer[occursin.(r"^[A-F].*[0-9]$", df_customer.status_cd), :]
first(df, 10)
```

<div><div style = "float: left;"><span>10×11 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">customer_name</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">gender</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">postal_cd</th><th style = "text-align: left;">address</th><th style = "text-align: left;">application_store_cd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">status_cd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS011215000048</td><td style = "text-align: left;">芦田 沙耶</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1992-02-01</td><td style = "text-align: right;">27</td><td style = "text-align: left;">223-0062</td><td style = "text-align: left;">神奈川県横浜市港北区日吉本町**********</td><td style = "text-align: left;">S14011</td><td style = "text-align: left;">20150228</td><td style = "text-align: left;">C-20100421-9</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS022513000105</td><td style = "text-align: left;">島村 貴美子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1962-03-12</td><td style = "text-align: right;">57</td><td style = "text-align: left;">249-0002</td><td style = "text-align: left;">神奈川県逗子市山の根**********</td><td style = "text-align: left;">S14022</td><td style = "text-align: left;">20150320</td><td style = "text-align: left;">A-20091115-7</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001515000096</td><td style = "text-align: left;">水野 陽子</td><td style = "text-align: left;">9</td><td style = "text-align: left;">不明</td><td style = "text-align: left;">1960-11-29</td><td style = "text-align: right;">58</td><td style = "text-align: left;">144-0053</td><td style = "text-align: left;">東京都大田区蒲田本町**********</td><td style = "text-align: left;">S13001</td><td style = "text-align: left;">20150614</td><td style = "text-align: left;">A-20100724-7</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS013615000053</td><td style = "text-align: left;">西脇 季衣</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1953-10-18</td><td style = "text-align: right;">65</td><td style = "text-align: left;">261-0026</td><td style = "text-align: left;">千葉県千葉市美浜区幕張西**********</td><td style = "text-align: left;">S12013</td><td style = "text-align: left;">20150128</td><td style = "text-align: left;">B-20100329-6</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS020412000161</td><td style = "text-align: left;">小宮 薫</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1974-05-21</td><td style = "text-align: right;">44</td><td style = "text-align: left;">174-0042</td><td style = "text-align: left;">東京都板橋区東坂下**********</td><td style = "text-align: left;">S13020</td><td style = "text-align: left;">20150822</td><td style = "text-align: left;">B-20081021-3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001215000097</td><td style = "text-align: left;">竹中 あさみ</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1990-07-25</td><td style = "text-align: right;">28</td><td style = "text-align: left;">146-0095</td><td style = "text-align: left;">東京都大田区多摩川**********</td><td style = "text-align: left;">S13001</td><td style = "text-align: left;">20170315</td><td style = "text-align: left;">A-20100211-2</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS035212000007</td><td style = "text-align: left;">内村 恵梨香</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1990-12-04</td><td style = "text-align: right;">28</td><td style = "text-align: left;">152-0023</td><td style = "text-align: left;">東京都目黒区八雲**********</td><td style = "text-align: left;">S13035</td><td style = "text-align: left;">20151013</td><td style = "text-align: left;">B-20101018-6</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS002515000386</td><td style = "text-align: left;">野田 コウ</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1963-05-30</td><td style = "text-align: right;">55</td><td style = "text-align: left;">185-0013</td><td style = "text-align: left;">東京都国分寺市西恋ケ窪**********</td><td style = "text-align: left;">S13002</td><td style = "text-align: left;">20160410</td><td style = "text-align: left;">C-20100127-8</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001615000372</td><td style = "text-align: left;">稲垣 寿々花</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1956-10-29</td><td style = "text-align: right;">62</td><td style = "text-align: left;">144-0035</td><td style = "text-align: left;">東京都大田区南蒲田**********</td><td style = "text-align: left;">S13001</td><td style = "text-align: left;">20170403</td><td style = "text-align: left;">A-20100104-1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS032512000121</td><td style = "text-align: left;">松井 知世</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1962-09-04</td><td style = "text-align: right;">56</td><td style = "text-align: left;">210-0011</td><td style = "text-align: left;">神奈川県川崎市川崎区富士見**********</td><td style = "text-align: left;">S13032</td><td style = "text-align: left;">20150727</td><td style = "text-align: left;">A-20100103-5</td></tr></tbody></table></div>

```julia
# 別解
df_customer |>
    @filter(occursin(r"^[A-F].*[0-9]$", _.status_cd)) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×11 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">customer_name</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">gender</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">postal_cd</th><th style = "text-align: left;">address</th><th style = "text-align: left;">application_store_cd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">status_cd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS011215000048</td><td style = "text-align: left;">芦田 沙耶</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1992-02-01</td><td style = "text-align: right;">27</td><td style = "text-align: left;">223-0062</td><td style = "text-align: left;">神奈川県横浜市港北区日吉本町**********</td><td style = "text-align: left;">S14011</td><td style = "text-align: left;">20150228</td><td style = "text-align: left;">C-20100421-9</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS022513000105</td><td style = "text-align: left;">島村 貴美子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1962-03-12</td><td style = "text-align: right;">57</td><td style = "text-align: left;">249-0002</td><td style = "text-align: left;">神奈川県逗子市山の根**********</td><td style = "text-align: left;">S14022</td><td style = "text-align: left;">20150320</td><td style = "text-align: left;">A-20091115-7</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001515000096</td><td style = "text-align: left;">水野 陽子</td><td style = "text-align: left;">9</td><td style = "text-align: left;">不明</td><td style = "text-align: left;">1960-11-29</td><td style = "text-align: right;">58</td><td style = "text-align: left;">144-0053</td><td style = "text-align: left;">東京都大田区蒲田本町**********</td><td style = "text-align: left;">S13001</td><td style = "text-align: left;">20150614</td><td style = "text-align: left;">A-20100724-7</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS013615000053</td><td style = "text-align: left;">西脇 季衣</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1953-10-18</td><td style = "text-align: right;">65</td><td style = "text-align: left;">261-0026</td><td style = "text-align: left;">千葉県千葉市美浜区幕張西**********</td><td style = "text-align: left;">S12013</td><td style = "text-align: left;">20150128</td><td style = "text-align: left;">B-20100329-6</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS020412000161</td><td style = "text-align: left;">小宮 薫</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1974-05-21</td><td style = "text-align: right;">44</td><td style = "text-align: left;">174-0042</td><td style = "text-align: left;">東京都板橋区東坂下**********</td><td style = "text-align: left;">S13020</td><td style = "text-align: left;">20150822</td><td style = "text-align: left;">B-20081021-3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001215000097</td><td style = "text-align: left;">竹中 あさみ</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1990-07-25</td><td style = "text-align: right;">28</td><td style = "text-align: left;">146-0095</td><td style = "text-align: left;">東京都大田区多摩川**********</td><td style = "text-align: left;">S13001</td><td style = "text-align: left;">20170315</td><td style = "text-align: left;">A-20100211-2</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS035212000007</td><td style = "text-align: left;">内村 恵梨香</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1990-12-04</td><td style = "text-align: right;">28</td><td style = "text-align: left;">152-0023</td><td style = "text-align: left;">東京都目黒区八雲**********</td><td style = "text-align: left;">S13035</td><td style = "text-align: left;">20151013</td><td style = "text-align: left;">B-20101018-6</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS002515000386</td><td style = "text-align: left;">野田 コウ</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1963-05-30</td><td style = "text-align: right;">55</td><td style = "text-align: left;">185-0013</td><td style = "text-align: left;">東京都国分寺市西恋ケ窪**********</td><td style = "text-align: left;">S13002</td><td style = "text-align: left;">20160410</td><td style = "text-align: left;">C-20100127-8</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001615000372</td><td style = "text-align: left;">稲垣 寿々花</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1956-10-29</td><td style = "text-align: right;">62</td><td style = "text-align: left;">144-0035</td><td style = "text-align: left;">東京都大田区南蒲田**********</td><td style = "text-align: left;">S13001</td><td style = "text-align: left;">20170403</td><td style = "text-align: left;">A-20100104-1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS032512000121</td><td style = "text-align: left;">松井 知世</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1962-09-04</td><td style = "text-align: right;">56</td><td style = "text-align: left;">210-0011</td><td style = "text-align: left;">神奈川県川崎市川崎区富士見**********</td><td style = "text-align: left;">S13032</td><td style = "text-align: left;">20150727</td><td style = "text-align: left;">A-20100103-5</td></tr></tbody></table></div>

---
> J-016: 店舗データ（df_store）から、電話番号（tel_no）が3桁-3桁-4桁のデータを全項目表示せよ。

```julia
df = df_store[occursin.(r"[0-9]{3}-[0-9]{3}-[0-9]{4}", df_store.tel_no), :]
first(df, 10)
```

<div><div style = "float: left;"><span>10×10 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">store_name</th><th style = "text-align: left;">prefecture_cd</th><th style = "text-align: left;">prefecture</th><th style = "text-align: left;">address</th><th style = "text-align: left;">address_kana</th><th style = "text-align: left;">tel_no</th><th style = "text-align: left;">longitude</th><th style = "text-align: left;">latitude</th><th style = "text-align: left;">floor_area</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String7" style = "text-align: left;">String7</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Float64" style = "text-align: left;">Float64</th><th title = "Float64" style = "text-align: left;">Float64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">S12014</td><td style = "text-align: left;">千草台店</td><td style = "text-align: left;">12</td><td style = "text-align: left;">千葉県</td><td style = "text-align: left;">千葉県千葉市稲毛区千草台一丁目</td><td style = "text-align: left;">チバケンチバシイナゲクチグサダイイッチョウメ</td><td style = "text-align: left;">043-123-4003</td><td style = "text-align: right;">140.118</td><td style = "text-align: right;">35.6356</td><td style = "text-align: right;">1698.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">S13002</td><td style = "text-align: left;">国分寺店</td><td style = "text-align: left;">13</td><td style = "text-align: left;">東京都</td><td style = "text-align: left;">東京都国分寺市本多二丁目</td><td style = "text-align: left;">トウキョウトコクブンジシホンダニチョウメ</td><td style = "text-align: left;">042-123-4008</td><td style = "text-align: right;">139.48</td><td style = "text-align: right;">35.7057</td><td style = "text-align: right;">1735.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">S14010</td><td style = "text-align: left;">菊名店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市港北区菊名一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシコウホククキクナイッチョウメ</td><td style = "text-align: left;">045-123-4032</td><td style = "text-align: right;">139.633</td><td style = "text-align: right;">35.5005</td><td style = "text-align: right;">1732.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">S14033</td><td style = "text-align: left;">阿久和店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市瀬谷区阿久和西一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシセヤクアクワニシイッチョウメ</td><td style = "text-align: left;">045-123-4043</td><td style = "text-align: right;">139.496</td><td style = "text-align: right;">35.4592</td><td style = "text-align: right;">1495.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">S14036</td><td style = "text-align: left;">相模原中央店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県相模原市中央二丁目</td><td style = "text-align: left;">カナガワケンサガミハラシチュウオウニチョウメ</td><td style = "text-align: left;">042-123-4045</td><td style = "text-align: right;">139.372</td><td style = "text-align: right;">35.5733</td><td style = "text-align: right;">1679.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">S14040</td><td style = "text-align: left;">長津田店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市緑区長津田みなみ台五丁目</td><td style = "text-align: left;">カナガワケンヨコハマシミドリクナガツタミナミダイゴチョウメ</td><td style = "text-align: left;">045-123-4046</td><td style = "text-align: right;">139.499</td><td style = "text-align: right;">35.524</td><td style = "text-align: right;">1548.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">S14050</td><td style = "text-align: left;">阿久和西店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市瀬谷区阿久和西一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシセヤクアクワニシイッチョウメ</td><td style = "text-align: left;">045-123-4053</td><td style = "text-align: right;">139.496</td><td style = "text-align: right;">35.4592</td><td style = "text-align: right;">1830.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">S13052</td><td style = "text-align: left;">森野店</td><td style = "text-align: left;">13</td><td style = "text-align: left;">東京都</td><td style = "text-align: left;">東京都町田市森野三丁目</td><td style = "text-align: left;">トウキョウトマチダシモリノサンチョウメ</td><td style = "text-align: left;">042-123-4030</td><td style = "text-align: right;">139.438</td><td style = "text-align: right;">35.5529</td><td style = "text-align: right;">1087.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">S14028</td><td style = "text-align: left;">二ツ橋店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市瀬谷区二ツ橋町</td><td style = "text-align: left;">カナガワケンヨコハマシセヤクフタツバシチョウ</td><td style = "text-align: left;">045-123-4042</td><td style = "text-align: right;">139.496</td><td style = "text-align: right;">35.463</td><td style = "text-align: right;">1574.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">S14012</td><td style = "text-align: left;">本牧和田店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市中区本牧和田</td><td style = "text-align: left;">カナガワケンヨコハマシナカクホンモクワダ</td><td style = "text-align: left;">045-123-4034</td><td style = "text-align: right;">139.658</td><td style = "text-align: right;">35.4216</td><td style = "text-align: right;">1341.0</td></tr></tbody></table></div>

```julia
# 別解
df_store |>
    @filter(occursin(r"[0-9]{3}-[0-9]{3}-[0-9]{4}", _.tel_no)) |>
    DataFrame
```

<div><div style = "float: left;"><span>34×10 DataFrame</span></div><div style = "float: right;"><span style = "font-style: italic;">9 rows omitted</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">store_name</th><th style = "text-align: left;">prefecture_cd</th><th style = "text-align: left;">prefecture</th><th style = "text-align: left;">address</th><th style = "text-align: left;">address_kana</th><th style = "text-align: left;">tel_no</th><th style = "text-align: left;">longitude</th><th style = "text-align: left;">latitude</th><th style = "text-align: left;">floor_area</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String7" style = "text-align: left;">String7</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Float64" style = "text-align: left;">Float64</th><th title = "Float64" style = "text-align: left;">Float64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">S12014</td><td style = "text-align: left;">千草台店</td><td style = "text-align: left;">12</td><td style = "text-align: left;">千葉県</td><td style = "text-align: left;">千葉県千葉市稲毛区千草台一丁目</td><td style = "text-align: left;">チバケンチバシイナゲクチグサダイイッチョウメ</td><td style = "text-align: left;">043-123-4003</td><td style = "text-align: right;">140.118</td><td style = "text-align: right;">35.6356</td><td style = "text-align: right;">1698.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">S13002</td><td style = "text-align: left;">国分寺店</td><td style = "text-align: left;">13</td><td style = "text-align: left;">東京都</td><td style = "text-align: left;">東京都国分寺市本多二丁目</td><td style = "text-align: left;">トウキョウトコクブンジシホンダニチョウメ</td><td style = "text-align: left;">042-123-4008</td><td style = "text-align: right;">139.48</td><td style = "text-align: right;">35.7057</td><td style = "text-align: right;">1735.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">S14010</td><td style = "text-align: left;">菊名店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市港北区菊名一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシコウホククキクナイッチョウメ</td><td style = "text-align: left;">045-123-4032</td><td style = "text-align: right;">139.633</td><td style = "text-align: right;">35.5005</td><td style = "text-align: right;">1732.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">S14033</td><td style = "text-align: left;">阿久和店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市瀬谷区阿久和西一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシセヤクアクワニシイッチョウメ</td><td style = "text-align: left;">045-123-4043</td><td style = "text-align: right;">139.496</td><td style = "text-align: right;">35.4592</td><td style = "text-align: right;">1495.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">S14036</td><td style = "text-align: left;">相模原中央店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県相模原市中央二丁目</td><td style = "text-align: left;">カナガワケンサガミハラシチュウオウニチョウメ</td><td style = "text-align: left;">042-123-4045</td><td style = "text-align: right;">139.372</td><td style = "text-align: right;">35.5733</td><td style = "text-align: right;">1679.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">S14040</td><td style = "text-align: left;">長津田店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市緑区長津田みなみ台五丁目</td><td style = "text-align: left;">カナガワケンヨコハマシミドリクナガツタミナミダイゴチョウメ</td><td style = "text-align: left;">045-123-4046</td><td style = "text-align: right;">139.499</td><td style = "text-align: right;">35.524</td><td style = "text-align: right;">1548.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">S14050</td><td style = "text-align: left;">阿久和西店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市瀬谷区阿久和西一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシセヤクアクワニシイッチョウメ</td><td style = "text-align: left;">045-123-4053</td><td style = "text-align: right;">139.496</td><td style = "text-align: right;">35.4592</td><td style = "text-align: right;">1830.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">S13052</td><td style = "text-align: left;">森野店</td><td style = "text-align: left;">13</td><td style = "text-align: left;">東京都</td><td style = "text-align: left;">東京都町田市森野三丁目</td><td style = "text-align: left;">トウキョウトマチダシモリノサンチョウメ</td><td style = "text-align: left;">042-123-4030</td><td style = "text-align: right;">139.438</td><td style = "text-align: right;">35.5529</td><td style = "text-align: right;">1087.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">S14028</td><td style = "text-align: left;">二ツ橋店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市瀬谷区二ツ橋町</td><td style = "text-align: left;">カナガワケンヨコハマシセヤクフタツバシチョウ</td><td style = "text-align: left;">045-123-4042</td><td style = "text-align: right;">139.496</td><td style = "text-align: right;">35.463</td><td style = "text-align: right;">1574.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">S14012</td><td style = "text-align: left;">本牧和田店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市中区本牧和田</td><td style = "text-align: left;">カナガワケンヨコハマシナカクホンモクワダ</td><td style = "text-align: left;">045-123-4034</td><td style = "text-align: right;">139.658</td><td style = "text-align: right;">35.4216</td><td style = "text-align: right;">1341.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">11</td><td style = "text-align: left;">S14046</td><td style = "text-align: left;">北山田店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市都筑区北山田一丁目</td><td style = "text-align: left;">カナガワケンヨコハマシツヅキクキタヤマタイッチョウメ</td><td style = "text-align: left;">045-123-4049</td><td style = "text-align: right;">139.592</td><td style = "text-align: right;">35.5619</td><td style = "text-align: right;">831.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">12</td><td style = "text-align: left;">S14022</td><td style = "text-align: left;">逗子店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県逗子市逗子一丁目</td><td style = "text-align: left;">カナガワケンズシシズシイッチョウメ</td><td style = "text-align: left;">046-123-4036</td><td style = "text-align: right;">139.579</td><td style = "text-align: right;">35.2964</td><td style = "text-align: right;">1838.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">13</td><td style = "text-align: left;">S14011</td><td style = "text-align: left;">日吉本町店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市港北区日吉本町四丁目</td><td style = "text-align: left;">カナガワケンヨコハマシコウホククヒヨシホンチョウヨンチョウメ</td><td style = "text-align: left;">045-123-4033</td><td style = "text-align: right;">139.632</td><td style = "text-align: right;">35.5466</td><td style = "text-align: right;">890.0</td></tr><tr><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">23</td><td style = "text-align: left;">S14027</td><td style = "text-align: left;">南藤沢店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県藤沢市南藤沢</td><td style = "text-align: left;">カナガワケンフジサワシミナミフジサワ</td><td style = "text-align: left;">046-123-4041</td><td style = "text-align: right;">139.49</td><td style = "text-align: right;">35.3376</td><td style = "text-align: right;">1521.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">24</td><td style = "text-align: left;">S14021</td><td style = "text-align: left;">伊勢原店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県伊勢原市伊勢原四丁目</td><td style = "text-align: left;">カナガワケンイセハラシイセハラヨンチョウメ</td><td style = "text-align: left;">046-123-4035</td><td style = "text-align: right;">139.313</td><td style = "text-align: right;">35.4017</td><td style = "text-align: right;">962.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">25</td><td style = "text-align: left;">S14047</td><td style = "text-align: left;">相模原店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県相模原市千代田六丁目</td><td style = "text-align: left;">カナガワケンサガミハラシチヨダロクチョウメ</td><td style = "text-align: left;">042-123-4050</td><td style = "text-align: right;">139.375</td><td style = "text-align: right;">35.5596</td><td style = "text-align: right;">1047.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">26</td><td style = "text-align: left;">S12013</td><td style = "text-align: left;">習志野店</td><td style = "text-align: left;">12</td><td style = "text-align: left;">千葉県</td><td style = "text-align: left;">千葉県習志野市芝園一丁目</td><td style = "text-align: left;">チバケンナラシノシシバゾノイッチョウメ</td><td style = "text-align: left;">047-123-4002</td><td style = "text-align: right;">140.022</td><td style = "text-align: right;">35.6612</td><td style = "text-align: right;">808.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">27</td><td style = "text-align: left;">S14042</td><td style = "text-align: left;">新山下店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市中区新山下二丁目</td><td style = "text-align: left;">カナガワケンヨコハマシナカクシンヤマシタニチョウメ</td><td style = "text-align: left;">045-123-4047</td><td style = "text-align: right;">139.659</td><td style = "text-align: right;">35.4389</td><td style = "text-align: right;">1044.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">28</td><td style = "text-align: left;">S12030</td><td style = "text-align: left;">八幡店</td><td style = "text-align: left;">12</td><td style = "text-align: left;">千葉県</td><td style = "text-align: left;">千葉県市川市八幡三丁目</td><td style = "text-align: left;">チバケンイチカワシヤワタサンチョウメ</td><td style = "text-align: left;">047-123-4005</td><td style = "text-align: right;">139.924</td><td style = "text-align: right;">35.7232</td><td style = "text-align: right;">1162.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">29</td><td style = "text-align: left;">S14025</td><td style = "text-align: left;">大和店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県大和市下和田</td><td style = "text-align: left;">カナガワケンヤマトシシモワダ</td><td style = "text-align: left;">046-123-4039</td><td style = "text-align: right;">139.468</td><td style = "text-align: right;">35.4341</td><td style = "text-align: right;">1011.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">30</td><td style = "text-align: left;">S14045</td><td style = "text-align: left;">厚木店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県厚木市中町二丁目</td><td style = "text-align: left;">カナガワケンアツギシナカチョウニチョウメ</td><td style = "text-align: left;">046-123-4048</td><td style = "text-align: right;">139.365</td><td style = "text-align: right;">35.4418</td><td style = "text-align: right;">980.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">31</td><td style = "text-align: left;">S12029</td><td style = "text-align: left;">東野店</td><td style = "text-align: left;">12</td><td style = "text-align: left;">千葉県</td><td style = "text-align: left;">千葉県浦安市東野一丁目</td><td style = "text-align: left;">チバケンウラヤスシヒガシノイッチョウメ</td><td style = "text-align: left;">047-123-4004</td><td style = "text-align: right;">139.897</td><td style = "text-align: right;">35.6509</td><td style = "text-align: right;">1101.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">32</td><td style = "text-align: left;">S12053</td><td style = "text-align: left;">高洲店</td><td style = "text-align: left;">12</td><td style = "text-align: left;">千葉県</td><td style = "text-align: left;">千葉県浦安市高洲五丁目</td><td style = "text-align: left;">チバケンウラヤスシタカスゴチョウメ</td><td style = "text-align: left;">047-123-4006</td><td style = "text-align: right;">139.918</td><td style = "text-align: right;">35.6375</td><td style = "text-align: right;">1555.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">33</td><td style = "text-align: left;">S14024</td><td style = "text-align: left;">三田店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県川崎市多摩区三田四丁目</td><td style = "text-align: left;">カナガワケンカワサキシタマクミタヨンチョウメ</td><td style = "text-align: left;">044-123-4038</td><td style = "text-align: right;">139.542</td><td style = "text-align: right;">35.6077</td><td style = "text-align: right;">972.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">34</td><td style = "text-align: left;">S14006</td><td style = "text-align: left;">葛が谷店</td><td style = "text-align: left;">14</td><td style = "text-align: left;">神奈川県</td><td style = "text-align: left;">神奈川県横浜市都筑区葛が谷</td><td style = "text-align: left;">カナガワケンヨコハマシツヅキククズガヤ</td><td style = "text-align: left;">045-123-4031</td><td style = "text-align: right;">139.563</td><td style = "text-align: right;">35.5357</td><td style = "text-align: right;">1886.0</td></tr></tbody></table></div>

# データフレームの並べ替え(sort)

## 5.　列でソート

---
> J-017: 顧客データ（df_customer）を生年月日（birth_day）で高齢順にソートし、先頭から全項目を10件表示せよ。

```julia
df = sort(df_customer, :birth_day)
first(df, 10)
```

<div><div style = "float: left;"><span>10×11 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">customer_name</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">gender</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">postal_cd</th><th style = "text-align: left;">address</th><th style = "text-align: left;">application_store_cd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">status_cd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS003813000014</td><td style = "text-align: left;">村山 菜々美</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1928-11-26</td><td style = "text-align: right;">90</td><td style = "text-align: left;">182-0007</td><td style = "text-align: left;">東京都調布市菊野台**********</td><td style = "text-align: left;">S13003</td><td style = "text-align: left;">20160214</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS026813000004</td><td style = "text-align: left;">吉村 朝陽</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1928-12-14</td><td style = "text-align: right;">90</td><td style = "text-align: left;">251-0043</td><td style = "text-align: left;">神奈川県藤沢市辻堂元町**********</td><td style = "text-align: left;">S14026</td><td style = "text-align: left;">20150723</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS018811000003</td><td style = "text-align: left;">熊沢 美里</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1929-01-07</td><td style = "text-align: right;">90</td><td style = "text-align: left;">204-0004</td><td style = "text-align: left;">東京都清瀬市野塩**********</td><td style = "text-align: left;">S13018</td><td style = "text-align: left;">20150403</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS027803000004</td><td style = "text-align: left;">内村 拓郎</td><td style = "text-align: left;">0</td><td style = "text-align: left;">男性</td><td style = "text-align: left;">1929-01-12</td><td style = "text-align: right;">90</td><td style = "text-align: left;">251-0031</td><td style = "text-align: left;">神奈川県藤沢市鵠沼藤が谷**********</td><td style = "text-align: left;">S14027</td><td style = "text-align: left;">20151227</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS013801000003</td><td style = "text-align: left;">天野 拓郎</td><td style = "text-align: left;">0</td><td style = "text-align: left;">男性</td><td style = "text-align: left;">1929-01-15</td><td style = "text-align: right;">90</td><td style = "text-align: left;">274-0824</td><td style = "text-align: left;">千葉県船橋市前原東**********</td><td style = "text-align: left;">S12013</td><td style = "text-align: left;">20160120</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001814000022</td><td style = "text-align: left;">鶴田 里穂</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1929-01-28</td><td style = "text-align: right;">90</td><td style = "text-align: left;">144-0045</td><td style = "text-align: left;">東京都大田区南六郷**********</td><td style = "text-align: left;">S13001</td><td style = "text-align: left;">20161012</td><td style = "text-align: left;">A-20090415-7</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS016815000002</td><td style = "text-align: left;">山元 美紀</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1929-02-22</td><td style = "text-align: right;">90</td><td style = "text-align: left;">184-0005</td><td style = "text-align: left;">東京都小金井市桜町**********</td><td style = "text-align: left;">S13016</td><td style = "text-align: left;">20150629</td><td style = "text-align: left;">C-20090923-C</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS009815000003</td><td style = "text-align: left;">中田 里穂</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1929-04-08</td><td style = "text-align: right;">89</td><td style = "text-align: left;">154-0014</td><td style = "text-align: left;">東京都世田谷区新町**********</td><td style = "text-align: left;">S13009</td><td style = "text-align: left;">20150421</td><td style = "text-align: left;">D-20091021-E</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS012813000013</td><td style = "text-align: left;">宇野 南朋</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1929-04-09</td><td style = "text-align: right;">89</td><td style = "text-align: left;">231-0806</td><td style = "text-align: left;">神奈川県横浜市中区本牧町**********</td><td style = "text-align: left;">S14012</td><td style = "text-align: left;">20150712</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS005813000015</td><td style = "text-align: left;">金谷 恵梨香</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1929-04-09</td><td style = "text-align: right;">89</td><td style = "text-align: left;">165-0032</td><td style = "text-align: left;">東京都中野区鷺宮**********</td><td style = "text-align: left;">S13005</td><td style = "text-align: left;">20150506</td><td style = "text-align: left;">0-00000000-0</td></tr></tbody></table></div>

```julia
# 別解
df_customer |>
    @orderby(_.birth_day) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×11 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">customer_name</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">gender</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">postal_cd</th><th style = "text-align: left;">address</th><th style = "text-align: left;">application_store_cd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">status_cd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS003813000014</td><td style = "text-align: left;">村山 菜々美</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1928-11-26</td><td style = "text-align: right;">90</td><td style = "text-align: left;">182-0007</td><td style = "text-align: left;">東京都調布市菊野台**********</td><td style = "text-align: left;">S13003</td><td style = "text-align: left;">20160214</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS026813000004</td><td style = "text-align: left;">吉村 朝陽</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1928-12-14</td><td style = "text-align: right;">90</td><td style = "text-align: left;">251-0043</td><td style = "text-align: left;">神奈川県藤沢市辻堂元町**********</td><td style = "text-align: left;">S14026</td><td style = "text-align: left;">20150723</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS018811000003</td><td style = "text-align: left;">熊沢 美里</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1929-01-07</td><td style = "text-align: right;">90</td><td style = "text-align: left;">204-0004</td><td style = "text-align: left;">東京都清瀬市野塩**********</td><td style = "text-align: left;">S13018</td><td style = "text-align: left;">20150403</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS027803000004</td><td style = "text-align: left;">内村 拓郎</td><td style = "text-align: left;">0</td><td style = "text-align: left;">男性</td><td style = "text-align: left;">1929-01-12</td><td style = "text-align: right;">90</td><td style = "text-align: left;">251-0031</td><td style = "text-align: left;">神奈川県藤沢市鵠沼藤が谷**********</td><td style = "text-align: left;">S14027</td><td style = "text-align: left;">20151227</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS013801000003</td><td style = "text-align: left;">天野 拓郎</td><td style = "text-align: left;">0</td><td style = "text-align: left;">男性</td><td style = "text-align: left;">1929-01-15</td><td style = "text-align: right;">90</td><td style = "text-align: left;">274-0824</td><td style = "text-align: left;">千葉県船橋市前原東**********</td><td style = "text-align: left;">S12013</td><td style = "text-align: left;">20160120</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001814000022</td><td style = "text-align: left;">鶴田 里穂</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1929-01-28</td><td style = "text-align: right;">90</td><td style = "text-align: left;">144-0045</td><td style = "text-align: left;">東京都大田区南六郷**********</td><td style = "text-align: left;">S13001</td><td style = "text-align: left;">20161012</td><td style = "text-align: left;">A-20090415-7</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS016815000002</td><td style = "text-align: left;">山元 美紀</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1929-02-22</td><td style = "text-align: right;">90</td><td style = "text-align: left;">184-0005</td><td style = "text-align: left;">東京都小金井市桜町**********</td><td style = "text-align: left;">S13016</td><td style = "text-align: left;">20150629</td><td style = "text-align: left;">C-20090923-C</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS009815000003</td><td style = "text-align: left;">中田 里穂</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1929-04-08</td><td style = "text-align: right;">89</td><td style = "text-align: left;">154-0014</td><td style = "text-align: left;">東京都世田谷区新町**********</td><td style = "text-align: left;">S13009</td><td style = "text-align: left;">20150421</td><td style = "text-align: left;">D-20091021-E</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS012813000013</td><td style = "text-align: left;">宇野 南朋</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1929-04-09</td><td style = "text-align: right;">89</td><td style = "text-align: left;">231-0806</td><td style = "text-align: left;">神奈川県横浜市中区本牧町**********</td><td style = "text-align: left;">S14012</td><td style = "text-align: left;">20150712</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS005813000015</td><td style = "text-align: left;">金谷 恵梨香</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1929-04-09</td><td style = "text-align: right;">89</td><td style = "text-align: left;">165-0032</td><td style = "text-align: left;">東京都中野区鷺宮**********</td><td style = "text-align: left;">S13005</td><td style = "text-align: left;">20150506</td><td style = "text-align: left;">0-00000000-0</td></tr></tbody></table></div>

---
> J-018: 顧客データ（df_customer）を生年月日（birth_day）で若い順にソートし、先頭から全項目を10件表示せよ。

**降順にソートするには `ref=true` を指定する。**

```julia
df = sort(df_customer, :birth_day, rev=true)
first(df, 10)
```

<div><div style = "float: left;"><span>10×11 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">customer_name</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">gender</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">postal_cd</th><th style = "text-align: left;">address</th><th style = "text-align: left;">application_store_cd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">status_cd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS035114000004</td><td style = "text-align: left;">大村 美里</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">2007-11-25</td><td style = "text-align: right;">11</td><td style = "text-align: left;">156-0053</td><td style = "text-align: left;">東京都世田谷区桜**********</td><td style = "text-align: left;">S13035</td><td style = "text-align: left;">20150619</td><td style = "text-align: left;">6-20091205-6</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS022103000002</td><td style = "text-align: left;">福山 はじめ</td><td style = "text-align: left;">9</td><td style = "text-align: left;">不明</td><td style = "text-align: left;">2007-10-02</td><td style = "text-align: right;">11</td><td style = "text-align: left;">249-0006</td><td style = "text-align: left;">神奈川県逗子市逗子**********</td><td style = "text-align: left;">S14022</td><td style = "text-align: left;">20160909</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS002113000009</td><td style = "text-align: left;">柴田 真悠子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">2007-09-17</td><td style = "text-align: right;">11</td><td style = "text-align: left;">184-0014</td><td style = "text-align: left;">東京都小金井市貫井南町**********</td><td style = "text-align: left;">S13002</td><td style = "text-align: left;">20160304</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS004115000014</td><td style = "text-align: left;">松井 京子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">2007-08-09</td><td style = "text-align: right;">11</td><td style = "text-align: left;">165-0031</td><td style = "text-align: left;">東京都中野区上鷺宮**********</td><td style = "text-align: left;">S13004</td><td style = "text-align: left;">20161120</td><td style = "text-align: left;">1-20081231-1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS002114000010</td><td style = "text-align: left;">山内 遥</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">2007-06-03</td><td style = "text-align: right;">11</td><td style = "text-align: left;">184-0015</td><td style = "text-align: left;">東京都小金井市貫井北町**********</td><td style = "text-align: left;">S13002</td><td style = "text-align: left;">20160920</td><td style = "text-align: left;">6-20100510-1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS025115000002</td><td style = "text-align: left;">小柳 夏希</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">2007-04-18</td><td style = "text-align: right;">11</td><td style = "text-align: left;">245-0018</td><td style = "text-align: left;">神奈川県横浜市泉区上飯田町**********</td><td style = "text-align: left;">S14025</td><td style = "text-align: left;">20160116</td><td style = "text-align: left;">D-20100913-D</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS002113000025</td><td style = "text-align: left;">広末 まなみ</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">2007-03-30</td><td style = "text-align: right;">12</td><td style = "text-align: left;">184-0015</td><td style = "text-align: left;">東京都小金井市貫井北町**********</td><td style = "text-align: left;">S13002</td><td style = "text-align: left;">20171030</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS033112000003</td><td style = "text-align: left;">長野 美紀</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">2007-03-22</td><td style = "text-align: right;">12</td><td style = "text-align: left;">245-0051</td><td style = "text-align: left;">神奈川県横浜市戸塚区名瀬町**********</td><td style = "text-align: left;">S14033</td><td style = "text-align: left;">20150606</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS007115000006</td><td style = "text-align: left;">福岡 瞬</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">2007-03-10</td><td style = "text-align: right;">12</td><td style = "text-align: left;">285-0845</td><td style = "text-align: left;">千葉県佐倉市西志津**********</td><td style = "text-align: left;">S12007</td><td style = "text-align: left;">20151118</td><td style = "text-align: left;">F-20101016-F</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS014113000008</td><td style = "text-align: left;">矢口 莉緒</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">2007-03-05</td><td style = "text-align: right;">12</td><td style = "text-align: left;">260-0041</td><td style = "text-align: left;">千葉県千葉市中央区東千葉**********</td><td style = "text-align: left;">S12014</td><td style = "text-align: left;">20150622</td><td style = "text-align: left;">3-20091108-6</td></tr></tbody></table></div>

```julia
# 別解
df_customer |>
    @orderby_descending(_.birth_day) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×11 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">customer_name</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">gender</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">postal_cd</th><th style = "text-align: left;">address</th><th style = "text-align: left;">application_store_cd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">status_cd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS035114000004</td><td style = "text-align: left;">大村 美里</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">2007-11-25</td><td style = "text-align: right;">11</td><td style = "text-align: left;">156-0053</td><td style = "text-align: left;">東京都世田谷区桜**********</td><td style = "text-align: left;">S13035</td><td style = "text-align: left;">20150619</td><td style = "text-align: left;">6-20091205-6</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS022103000002</td><td style = "text-align: left;">福山 はじめ</td><td style = "text-align: left;">9</td><td style = "text-align: left;">不明</td><td style = "text-align: left;">2007-10-02</td><td style = "text-align: right;">11</td><td style = "text-align: left;">249-0006</td><td style = "text-align: left;">神奈川県逗子市逗子**********</td><td style = "text-align: left;">S14022</td><td style = "text-align: left;">20160909</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS002113000009</td><td style = "text-align: left;">柴田 真悠子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">2007-09-17</td><td style = "text-align: right;">11</td><td style = "text-align: left;">184-0014</td><td style = "text-align: left;">東京都小金井市貫井南町**********</td><td style = "text-align: left;">S13002</td><td style = "text-align: left;">20160304</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS004115000014</td><td style = "text-align: left;">松井 京子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">2007-08-09</td><td style = "text-align: right;">11</td><td style = "text-align: left;">165-0031</td><td style = "text-align: left;">東京都中野区上鷺宮**********</td><td style = "text-align: left;">S13004</td><td style = "text-align: left;">20161120</td><td style = "text-align: left;">1-20081231-1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS002114000010</td><td style = "text-align: left;">山内 遥</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">2007-06-03</td><td style = "text-align: right;">11</td><td style = "text-align: left;">184-0015</td><td style = "text-align: left;">東京都小金井市貫井北町**********</td><td style = "text-align: left;">S13002</td><td style = "text-align: left;">20160920</td><td style = "text-align: left;">6-20100510-1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS025115000002</td><td style = "text-align: left;">小柳 夏希</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">2007-04-18</td><td style = "text-align: right;">11</td><td style = "text-align: left;">245-0018</td><td style = "text-align: left;">神奈川県横浜市泉区上飯田町**********</td><td style = "text-align: left;">S14025</td><td style = "text-align: left;">20160116</td><td style = "text-align: left;">D-20100913-D</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS002113000025</td><td style = "text-align: left;">広末 まなみ</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">2007-03-30</td><td style = "text-align: right;">12</td><td style = "text-align: left;">184-0015</td><td style = "text-align: left;">東京都小金井市貫井北町**********</td><td style = "text-align: left;">S13002</td><td style = "text-align: left;">20171030</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS033112000003</td><td style = "text-align: left;">長野 美紀</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">2007-03-22</td><td style = "text-align: right;">12</td><td style = "text-align: left;">245-0051</td><td style = "text-align: left;">神奈川県横浜市戸塚区名瀬町**********</td><td style = "text-align: left;">S14033</td><td style = "text-align: left;">20150606</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS007115000006</td><td style = "text-align: left;">福岡 瞬</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">2007-03-10</td><td style = "text-align: right;">12</td><td style = "text-align: left;">285-0845</td><td style = "text-align: left;">千葉県佐倉市西志津**********</td><td style = "text-align: left;">S12007</td><td style = "text-align: left;">20151118</td><td style = "text-align: left;">F-20101016-F</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS014113000008</td><td style = "text-align: left;">矢口 莉緒</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">2007-03-05</td><td style = "text-align: right;">12</td><td style = "text-align: left;">260-0041</td><td style = "text-align: left;">千葉県千葉市中央区東千葉**********</td><td style = "text-align: left;">S12014</td><td style = "text-align: left;">20150622</td><td style = "text-align: left;">3-20091108-6</td></tr></tbody></table></div>

## 6.　順位付けをしてソート

**順位付けは StatsBase パッケージに `tiedrank()`, `competerank()`, `ordinalrank()` がある。**

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

```julia
using StatsBase
df = select(df_receipt, [:customer_id, :amount])
df.rank = competerank(df_receipt.amount, rev=true)
df = sort(df, :rank)
first(df, 10)
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">amount</th><th style = "text-align: left;">rank</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS011415000006</td><td style = "text-align: right;">10925</td><td style = "text-align: right;">1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: right;">6800</td><td style = "text-align: right;">2</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS028605000002</td><td style = "text-align: right;">5780</td><td style = "text-align: right;">3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS015515000034</td><td style = "text-align: right;">5480</td><td style = "text-align: right;">4</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: right;">5480</td><td style = "text-align: right;">4</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: right;">5480</td><td style = "text-align: right;">4</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: right;">5440</td><td style = "text-align: right;">7</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS021515000089</td><td style = "text-align: right;">5440</td><td style = "text-align: right;">7</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS015515000083</td><td style = "text-align: right;">5280</td><td style = "text-align: right;">9</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS017414000114</td><td style = "text-align: right;">5280</td><td style = "text-align: right;">9</td></tr></tbody></table></div>

```julia
# 別解
using StatsBase

df_receipt |>
    x -> hcat(x, DataFrame(rank = competerank(df_receipt[:, :amount], rev=true))) |>
    @select(:customer_id, :amount, :rank) |>
    @orderby(_.rank) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">amount</th><th style = "text-align: left;">rank</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS011415000006</td><td style = "text-align: right;">10925</td><td style = "text-align: right;">1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: right;">6800</td><td style = "text-align: right;">2</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS028605000002</td><td style = "text-align: right;">5780</td><td style = "text-align: right;">3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS015515000034</td><td style = "text-align: right;">5480</td><td style = "text-align: right;">4</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: right;">5480</td><td style = "text-align: right;">4</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: right;">5480</td><td style = "text-align: right;">4</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: right;">5440</td><td style = "text-align: right;">7</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS021515000089</td><td style = "text-align: right;">5440</td><td style = "text-align: right;">7</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS015515000083</td><td style = "text-align: right;">5280</td><td style = "text-align: right;">9</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS017414000114</td><td style = "text-align: right;">5280</td><td style = "text-align: right;">9</td></tr></tbody></table></div>

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

```julia
using StatsBase
df = select(df_receipt, [:customer_id, :amount])
df.rank = ordinalrank(df_receipt.amount, rev=true)
df = sort(df, :rank)
first(df, 10)
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">amount</th><th style = "text-align: left;">rank</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS011415000006</td><td style = "text-align: right;">10925</td><td style = "text-align: right;">1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: right;">6800</td><td style = "text-align: right;">2</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS028605000002</td><td style = "text-align: right;">5780</td><td style = "text-align: right;">3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS015515000034</td><td style = "text-align: right;">5480</td><td style = "text-align: right;">4</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: right;">5480</td><td style = "text-align: right;">5</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: right;">5480</td><td style = "text-align: right;">6</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: right;">5440</td><td style = "text-align: right;">7</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS021515000089</td><td style = "text-align: right;">5440</td><td style = "text-align: right;">8</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS015515000083</td><td style = "text-align: right;">5280</td><td style = "text-align: right;">9</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS017414000114</td><td style = "text-align: right;">5280</td><td style = "text-align: right;">10</td></tr></tbody></table></div>

```julia
# 別解
df_receipt |>
    x -> hcat(x, DataFrame(rank = ordinalrank(df_receipt[:, :amount], rev=true))) |>
    @select(:customer_id, :amount, :rank) |>
    @orderby(_.rank) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">amount</th><th style = "text-align: left;">rank</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS011415000006</td><td style = "text-align: right;">10925</td><td style = "text-align: right;">1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: right;">6800</td><td style = "text-align: right;">2</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS028605000002</td><td style = "text-align: right;">5780</td><td style = "text-align: right;">3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS015515000034</td><td style = "text-align: right;">5480</td><td style = "text-align: right;">4</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: right;">5480</td><td style = "text-align: right;">5</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: right;">5480</td><td style = "text-align: right;">6</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: right;">5440</td><td style = "text-align: right;">7</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS021515000089</td><td style = "text-align: right;">5440</td><td style = "text-align: right;">8</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS015515000083</td><td style = "text-align: right;">5280</td><td style = "text-align: right;">9</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS017414000114</td><td style = "text-align: right;">5280</td><td style = "text-align: right;">10</td></tr></tbody></table></div>

# 件数（行数）の確認

---
> J-021: レシート明細データ（df_receipt）に対し、件数をカウントせよ。

```julia
nrow(df_receipt)
```

    104681

```julia
# 別解
df_receipt |> nrow
```

    104681

---
> J-022: レシート明細データ（df_receipt）の顧客ID（customer_id）に対し、ユニーク件数をカウントせよ。

**ユニークなデータ，データフレームを作るには `unique()` を使う。**

```julia
length(unique(df_receipt.customer_id))
```

    8307

```julia
# 別解
df_receipt |>
    @unique(_.customer_id) |>
    collect |>
    length
```

    8307

```julia
# 別解2
df_receipt |>
    @select(:customer_id) |>
    @unique() |>
    DataFrame |>
    nrow
```

    8307

```julia
# 別解3
df_receipt |>
    @select(:customer_id) |>
    @unique() |>
    collect |>
    length
```

    8307

# グループ化して処理

**groupby() によってグループ化して，それぞれのグループ化データフレームを `combine()` によって処理して，その結果をデータフレームとして生成する。**

## 7.　合計

---
> J-023: レシート明細データ（df_receipt）に対し、店舗コード（store_cd）ごとに売上金額（amount）と売上数量（quantity）を合計せよ。

```julia
gd = groupby(df_receipt, :store_cd);
df = combine(gd, :amount => sum => :amount, :quantity => sum => :quantity)
sort(df, :store_cd)
```

<div><div style = "float: left;"><span>52×3 DataFrame</span></div><div style = "float: right;"><span style = "font-style: italic;">27 rows omitted</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">amount</th><th style = "text-align: left;">quantity</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String7" style = "text-align: left;">String7</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">S12007</td><td style = "text-align: right;">638761</td><td style = "text-align: right;">2099</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">S12013</td><td style = "text-align: right;">787513</td><td style = "text-align: right;">2425</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">S12014</td><td style = "text-align: right;">725167</td><td style = "text-align: right;">2358</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">S12029</td><td style = "text-align: right;">794741</td><td style = "text-align: right;">2555</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">S12030</td><td style = "text-align: right;">684402</td><td style = "text-align: right;">2403</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">S13001</td><td style = "text-align: right;">811936</td><td style = "text-align: right;">2347</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">S13002</td><td style = "text-align: right;">727821</td><td style = "text-align: right;">2340</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">S13003</td><td style = "text-align: right;">764294</td><td style = "text-align: right;">2197</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">S13004</td><td style = "text-align: right;">779373</td><td style = "text-align: right;">2390</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">S13005</td><td style = "text-align: right;">629876</td><td style = "text-align: right;">2004</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">11</td><td style = "text-align: left;">S13008</td><td style = "text-align: right;">809288</td><td style = "text-align: right;">2491</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">12</td><td style = "text-align: left;">S13009</td><td style = "text-align: right;">808870</td><td style = "text-align: right;">2486</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">13</td><td style = "text-align: left;">S13015</td><td style = "text-align: right;">780873</td><td style = "text-align: right;">2248</td></tr><tr><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">41</td><td style = "text-align: left;">S14028</td><td style = "text-align: right;">786145</td><td style = "text-align: right;">2458</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">42</td><td style = "text-align: left;">S14033</td><td style = "text-align: right;">725318</td><td style = "text-align: right;">2282</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">43</td><td style = "text-align: left;">S14034</td><td style = "text-align: right;">653681</td><td style = "text-align: right;">2024</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">44</td><td style = "text-align: left;">S14036</td><td style = "text-align: right;">203694</td><td style = "text-align: right;">635</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">45</td><td style = "text-align: left;">S14040</td><td style = "text-align: right;">701858</td><td style = "text-align: right;">2233</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">46</td><td style = "text-align: left;">S14042</td><td style = "text-align: right;">534689</td><td style = "text-align: right;">1935</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">47</td><td style = "text-align: left;">S14045</td><td style = "text-align: right;">458484</td><td style = "text-align: right;">1398</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">48</td><td style = "text-align: left;">S14046</td><td style = "text-align: right;">412646</td><td style = "text-align: right;">1354</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">49</td><td style = "text-align: left;">S14047</td><td style = "text-align: right;">338329</td><td style = "text-align: right;">1041</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">50</td><td style = "text-align: left;">S14048</td><td style = "text-align: right;">234276</td><td style = "text-align: right;">769</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">51</td><td style = "text-align: left;">S14049</td><td style = "text-align: right;">230808</td><td style = "text-align: right;">788</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">52</td><td style = "text-align: left;">S14050</td><td style = "text-align: right;">167090</td><td style = "text-align: right;">580</td></tr></tbody></table></div>

```julia
# 別解
df_receipt |>
    @groupby(_.store_cd) |>
    @map({store_cd=key(_), amount=sum(_.amount), quantity=sum(_.quantity)}) |>
    @orderby(_.store_cd) |>
    DataFrame
```

<div><div style = "float: left;"><span>52×3 DataFrame</span></div><div style = "float: right;"><span style = "font-style: italic;">27 rows omitted</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">amount</th><th style = "text-align: left;">quantity</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String7" style = "text-align: left;">String7</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">S12007</td><td style = "text-align: right;">638761</td><td style = "text-align: right;">2099</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">S12013</td><td style = "text-align: right;">787513</td><td style = "text-align: right;">2425</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">S12014</td><td style = "text-align: right;">725167</td><td style = "text-align: right;">2358</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">S12029</td><td style = "text-align: right;">794741</td><td style = "text-align: right;">2555</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">S12030</td><td style = "text-align: right;">684402</td><td style = "text-align: right;">2403</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">S13001</td><td style = "text-align: right;">811936</td><td style = "text-align: right;">2347</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">S13002</td><td style = "text-align: right;">727821</td><td style = "text-align: right;">2340</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">S13003</td><td style = "text-align: right;">764294</td><td style = "text-align: right;">2197</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">S13004</td><td style = "text-align: right;">779373</td><td style = "text-align: right;">2390</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">S13005</td><td style = "text-align: right;">629876</td><td style = "text-align: right;">2004</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">11</td><td style = "text-align: left;">S13008</td><td style = "text-align: right;">809288</td><td style = "text-align: right;">2491</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">12</td><td style = "text-align: left;">S13009</td><td style = "text-align: right;">808870</td><td style = "text-align: right;">2486</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">13</td><td style = "text-align: left;">S13015</td><td style = "text-align: right;">780873</td><td style = "text-align: right;">2248</td></tr><tr><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">41</td><td style = "text-align: left;">S14028</td><td style = "text-align: right;">786145</td><td style = "text-align: right;">2458</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">42</td><td style = "text-align: left;">S14033</td><td style = "text-align: right;">725318</td><td style = "text-align: right;">2282</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">43</td><td style = "text-align: left;">S14034</td><td style = "text-align: right;">653681</td><td style = "text-align: right;">2024</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">44</td><td style = "text-align: left;">S14036</td><td style = "text-align: right;">203694</td><td style = "text-align: right;">635</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">45</td><td style = "text-align: left;">S14040</td><td style = "text-align: right;">701858</td><td style = "text-align: right;">2233</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">46</td><td style = "text-align: left;">S14042</td><td style = "text-align: right;">534689</td><td style = "text-align: right;">1935</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">47</td><td style = "text-align: left;">S14045</td><td style = "text-align: right;">458484</td><td style = "text-align: right;">1398</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">48</td><td style = "text-align: left;">S14046</td><td style = "text-align: right;">412646</td><td style = "text-align: right;">1354</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">49</td><td style = "text-align: left;">S14047</td><td style = "text-align: right;">338329</td><td style = "text-align: right;">1041</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">50</td><td style = "text-align: left;">S14048</td><td style = "text-align: right;">234276</td><td style = "text-align: right;">769</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">51</td><td style = "text-align: left;">S14049</td><td style = "text-align: right;">230808</td><td style = "text-align: right;">788</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">52</td><td style = "text-align: left;">S14050</td><td style = "text-align: right;">167090</td><td style = "text-align: right;">580</td></tr></tbody></table></div>

## 8.　最大値

---
> J-024: レシート明細データ（df_receipt）に対し、顧客ID（customer_id）ごとに最も新しい売上年月日（sales_ymd）を求め、10件表示せよ。

```julia
gd = groupby(df_receipt, :customer_id);
df = combine(gd, :sales_ymd => maximum)
df = sort(df, :customer_id)  # ソートする必要がある場合
first(df, 10)
```

<div><div style = "float: left;"><span>10×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sales_ymd_maximum</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: right;">20190308</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">20190731</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">20190405</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: right;">20190625</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">20190224</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001211000025</td><td style = "text-align: right;">20190322</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001212000027</td><td style = "text-align: right;">20170127</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001212000031</td><td style = "text-align: right;">20180906</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001212000046</td><td style = "text-align: right;">20170811</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001212000070</td><td style = "text-align: right;">20191018</td></tr></tbody></table></div>

```julia
# 別解
df_receipt |>
    @groupby(_.customer_id) |>
    @map({customer_id=key(_), sales_ymd_maximum=maximum(_.sales_ymd)}) |>
    @orderby(_.customer_id) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sales_ymd_maximum</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: right;">20190308</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">20190731</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">20190405</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: right;">20190625</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">20190224</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001211000025</td><td style = "text-align: right;">20190322</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001212000027</td><td style = "text-align: right;">20170127</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001212000031</td><td style = "text-align: right;">20180906</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001212000046</td><td style = "text-align: right;">20170811</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001212000070</td><td style = "text-align: right;">20191018</td></tr></tbody></table></div>

## 9.　最小値

---
> J-025: レシート明細データ（df_receipt）に対し、顧客ID（customer_id）ごとに最も古い売上年月日（sales_ymd）を求め、10件表示せよ。

```julia
gd = groupby(df_receipt, :customer_id);
df = combine(gd, :sales_ymd => minimum)
df2 = sort(df, :customer_id)
first(df2, 10)
```

<div><div style = "float: left;"><span>10×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sales_ymd_minimum</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: right;">20190308</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">20180503</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">20171228</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: right;">20170914</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">20180207</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001211000025</td><td style = "text-align: right;">20190322</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001212000027</td><td style = "text-align: right;">20170127</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001212000031</td><td style = "text-align: right;">20180906</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001212000046</td><td style = "text-align: right;">20170811</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001212000070</td><td style = "text-align: right;">20191018</td></tr></tbody></table></div>

```julia
# 別解
df_receipt |>
    @groupby(_.customer_id) |>
    @map({customer_id=key(_), sales_ymd_minimum=minimum(_.sales_ymd)}) |>
    @orderby(_.customer_id) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sales_ymd_minimum</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: right;">20190308</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">20180503</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">20171228</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: right;">20170914</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">20180207</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001211000025</td><td style = "text-align: right;">20190322</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001212000027</td><td style = "text-align: right;">20170127</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001212000031</td><td style = "text-align: right;">20180906</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001212000046</td><td style = "text-align: right;">20170811</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001212000070</td><td style = "text-align: right;">20191018</td></tr></tbody></table></div>

## 10.　比較演算，論理演算

---
> J-026: レシート明細データ（df_receipt）に対し、顧客ID（customer_id）ごとに最も新しい売上年月日（sales_ymd）と古い売上年月日を求め、両者が異なるデータを10件表示せよ。

```julia
gd = groupby(df_receipt, :customer_id);
df = combine(gd, :sales_ymd => maximum => :max_ymd, :sales_ymd => minimum => :min_ymd) |>
    @filter(_.min_ymd != _.max_ymd) |>
    @orderby(_.customer_id) |>
    DataFrame
first(df, 10)
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">max_ymd</th><th style = "text-align: left;">min_ymd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">20190731</td><td style = "text-align: right;">20180503</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">20190405</td><td style = "text-align: right;">20171228</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: right;">20190625</td><td style = "text-align: right;">20170914</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">20190224</td><td style = "text-align: right;">20180207</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001214000009</td><td style = "text-align: right;">20190902</td><td style = "text-align: right;">20170306</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001214000017</td><td style = "text-align: right;">20191006</td><td style = "text-align: right;">20180828</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001214000048</td><td style = "text-align: right;">20190929</td><td style = "text-align: right;">20171109</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001214000052</td><td style = "text-align: right;">20190617</td><td style = "text-align: right;">20180208</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001215000005</td><td style = "text-align: right;">20181021</td><td style = "text-align: right;">20170206</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001215000040</td><td style = "text-align: right;">20171022</td><td style = "text-align: right;">20170214</td></tr></tbody></table></div>

```julia
# 別解
df_receipt |>
    @groupby(_.customer_id) |>
    @map({customer_id=key(_), max_ymd=maximum(_.sales_ymd), min_ymd=minimum(_.sales_ymd)}) |>
    @filter(_.min_ymd != _.max_ymd) |>
    @orderby(_.customer_id) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">max_ymd</th><th style = "text-align: left;">min_ymd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">20190731</td><td style = "text-align: right;">20180503</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">20190405</td><td style = "text-align: right;">20171228</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: right;">20190625</td><td style = "text-align: right;">20170914</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">20190224</td><td style = "text-align: right;">20180207</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001214000009</td><td style = "text-align: right;">20190902</td><td style = "text-align: right;">20170306</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001214000017</td><td style = "text-align: right;">20191006</td><td style = "text-align: right;">20180828</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001214000048</td><td style = "text-align: right;">20190929</td><td style = "text-align: right;">20171109</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001214000052</td><td style = "text-align: right;">20190617</td><td style = "text-align: right;">20180208</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001215000005</td><td style = "text-align: right;">20181021</td><td style = "text-align: right;">20170206</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001215000040</td><td style = "text-align: right;">20171022</td><td style = "text-align: right;">20170214</td></tr></tbody></table></div>

## 11.　平均値

---
> J-027: レシート明細データ（df_receipt）に対し、店舗コード（store_cd）ごとに売上金額（amount）の平均を計算し、降順でTOP5を表示せよ。

```julia
using StatsBase

gd = groupby(df_receipt, :store_cd);
combine(gd, :amount => mean => :mean_amount) |>
    @orderby_descending(_.mean_amount) |>
    @take(5) |>
    DataFrame
```

<div><div style = "float: left;"><span>5×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">mean_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String7" style = "text-align: left;">String7</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">S13052</td><td style = "text-align: right;">402.867</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">S13015</td><td style = "text-align: right;">351.112</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">S13003</td><td style = "text-align: right;">350.916</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">S14010</td><td style = "text-align: right;">348.791</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">S13001</td><td style = "text-align: right;">348.47</td></tr></tbody></table></div>

```julia
# 別解
using StatsBase

df_receipt |>
    @groupby(_.store_cd) |>
    @map({store_cd=key(_), mean_amount=mean(_.amount)}) |>
    @orderby_descending(_.mean_amount) |>
    @take(5) |>
    DataFrame
```

<div><div style = "float: left;"><span>5×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">mean_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String7" style = "text-align: left;">String7</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">S13052</td><td style = "text-align: right;">402.867</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">S13015</td><td style = "text-align: right;">351.112</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">S13003</td><td style = "text-align: right;">350.916</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">S14010</td><td style = "text-align: right;">348.791</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">S13001</td><td style = "text-align: right;">348.47</td></tr></tbody></table></div>

## 12.　中央値

---
> J-028: レシート明細データ（df_receipt）に対し、店舗コード（store_cd）ごとに売上金額（amount）の中央値を計算し、降順でTOP5を表示せよ。

```julia
gd = groupby(df_receipt, :store_cd);
combine(gd, :amount => median => :median_amount) |>
    @orderby_descending(_.median_amount) |>
    @thenby(_.store_cd) |>
    @take(5) |>
    DataFrame
```

<div><div style = "float: left;"><span>5×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">median_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String7" style = "text-align: left;">String7</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">S13052</td><td style = "text-align: right;">190.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">S14010</td><td style = "text-align: right;">188.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">S14050</td><td style = "text-align: right;">185.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">S13003</td><td style = "text-align: right;">180.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">S13018</td><td style = "text-align: right;">180.0</td></tr></tbody></table></div>

## 13.　最頻値

---
> J-029: レシート明細データ（df_receipt）に対し、店舗コード（store_cd）ごとに商品コード（product_cd）の最頻値を求め、10件表示させよ。

```julia
gd = groupby(df_receipt, :store_cd);
combine(gd, :product_cd => mode) |>
    @orderby(_.store_cd) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">product_cd_mode</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String7" style = "text-align: left;">String7</th><th title = "String15" style = "text-align: left;">String15</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">S12007</td><td style = "text-align: left;">P060303001</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">S12013</td><td style = "text-align: left;">P060303001</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">S12014</td><td style = "text-align: left;">P060303001</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">S12029</td><td style = "text-align: left;">P060303001</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">S12030</td><td style = "text-align: left;">P060303001</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">S13001</td><td style = "text-align: left;">P060303001</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">S13002</td><td style = "text-align: left;">P060303001</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">S13003</td><td style = "text-align: left;">P071401001</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">S13004</td><td style = "text-align: left;">P060303001</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">S13005</td><td style = "text-align: left;">P040503001</td></tr></tbody></table></div>

```julia
# 別解
using StatsBase

df_receipt |>
    @groupby(_.store_cd) |>
    @map({store_cd=key(_), product_cd_mode=mode(_.product_cd)}) |>
    @orderby(_.store_cd) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">product_cd_mode</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String7" style = "text-align: left;">String7</th><th title = "String15" style = "text-align: left;">String15</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">S12007</td><td style = "text-align: left;">P060303001</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">S12013</td><td style = "text-align: left;">P060303001</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">S12014</td><td style = "text-align: left;">P060303001</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">S12029</td><td style = "text-align: left;">P060303001</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">S12030</td><td style = "text-align: left;">P060303001</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">S13001</td><td style = "text-align: left;">P060303001</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">S13002</td><td style = "text-align: left;">P060303001</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">S13003</td><td style = "text-align: left;">P071401001</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">S13004</td><td style = "text-align: left;">P060303001</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">S13005</td><td style = "text-align: left;">P040503001</td></tr></tbody></table></div>

## 14.　分散

---
> J-030: レシート明細データ（df_receipt）に対し、店舗コード（store_cd）ごとに売上金額（amount）の分散を計算し、降順で5件表示せよ。

```julia
gd = groupby(df_receipt, :store_cd);
combine(gd, :amount => (x -> var(x, corrected=false)) => :var_amount) |>
    @orderby_descending(_.var_amount) |>
    @take(5) |>
    DataFrame
```

<div><div style = "float: left;"><span>5×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">var_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String7" style = "text-align: left;">String7</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">S13052</td><td style = "text-align: right;">4.40089e5</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">S14011</td><td style = "text-align: right;">3.06315e5</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">S14034</td><td style = "text-align: right;">2.9692e5</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">S13001</td><td style = "text-align: right;">295432.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">S13015</td><td style = "text-align: right;">2.95294e5</td></tr></tbody></table></div>

```julia
# 別解
df_receipt |>
    @groupby(_.store_cd) |>
    @map({store_cd=key(_), var_amount=var(_.amount, corrected=false)}) |>
    @orderby_descending(_.var_amount) |>
    @take(5) |>
    DataFrame
```

<div><div style = "float: left;"><span>5×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">var_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String7" style = "text-align: left;">String7</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">S13052</td><td style = "text-align: right;">4.40089e5</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">S14011</td><td style = "text-align: right;">3.06315e5</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">S14034</td><td style = "text-align: right;">2.9692e5</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">S13001</td><td style = "text-align: right;">295432.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">S13015</td><td style = "text-align: right;">2.95294e5</td></tr></tbody></table></div>

## 15.　標準偏差

---
> J-031: レシート明細データ（df_receipt）に対し、店舗コード（store_cd）ごとに売上金額（amount）の標準偏差を計算し、降順で5件表示せよ。

```julia
gd = groupby(df_receipt, :store_cd);
combine(gd, :amount => (x -> std(x, corrected=false)) => :std_amount) |>
    @orderby_descending(_.std_amount) |>
    @take(5) |>
    DataFrame
```

<div><div style = "float: left;"><span>5×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">std_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String7" style = "text-align: left;">String7</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">S13052</td><td style = "text-align: right;">663.392</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">S14011</td><td style = "text-align: right;">553.457</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">S14034</td><td style = "text-align: right;">544.904</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">S13001</td><td style = "text-align: right;">543.537</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">S13015</td><td style = "text-align: right;">543.41</td></tr></tbody></table></div>

```julia
# 別解
df_receipt |>
    @groupby(_.store_cd) |>
    @map({store_cd=key(_), std_amount=std(_.amount, corrected=false)}) |>
    @orderby_descending(_.std_amount) |>
    @take(5) |>
    DataFrame
```

<div><div style = "float: left;"><span>5×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">std_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String7" style = "text-align: left;">String7</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">S13052</td><td style = "text-align: right;">663.392</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">S14011</td><td style = "text-align: right;">553.457</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">S14034</td><td style = "text-align: right;">544.904</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">S13001</td><td style = "text-align: right;">543.537</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">S13015</td><td style = "text-align: right;">543.41</td></tr></tbody></table></div>

## 16.　パーセンタイル値（第1四分位数，中央値，第3四分位数）

---
> J-032: レシート明細データ（df_receipt）の売上金額（amount）について、25％刻みでパーセンタイル値を求めよ。

```julia
quantile(df_receipt.amount, 0.25:0.25:1)
```

    4-element Vector{Float64}:
       102.0
       170.0
       288.0
     10925.0

```julia
# 別解
combine(df_receipt, :amount => x -> quantile(x, 0.25:0.25:1))
```

<div><div style = "float: left;"><span>4×1 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">amount_function</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">102.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">170.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">288.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">10925.0</td></tr></tbody></table></div>

## 17.　比較演算，論理演算

---
> J-033: レシート明細データ（df_receipt）に対し、店舗コード（store_cd）ごとに売上金額（amount）の平均を計算し、330以上のものを抽出せよ。

```julia
gd = groupby(df_receipt, :store_cd);
combine(gd, :amount => mean => :mean_amount) |>
    @filter(_.mean_amount >= 330) |>
    @orderby(_.store_cd) |>
    DataFrame
```

<div><div style = "float: left;"><span>13×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">mean_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String7" style = "text-align: left;">String7</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">S12013</td><td style = "text-align: right;">330.194</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">S13001</td><td style = "text-align: right;">348.47</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">S13003</td><td style = "text-align: right;">350.916</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">S13004</td><td style = "text-align: right;">330.944</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">S13015</td><td style = "text-align: right;">351.112</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">S13019</td><td style = "text-align: right;">330.209</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">S13020</td><td style = "text-align: right;">337.88</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">S13052</td><td style = "text-align: right;">402.867</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">S14010</td><td style = "text-align: right;">348.791</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">S14011</td><td style = "text-align: right;">335.718</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">11</td><td style = "text-align: left;">S14026</td><td style = "text-align: right;">332.341</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">12</td><td style = "text-align: left;">S14045</td><td style = "text-align: right;">330.082</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">13</td><td style = "text-align: left;">S14047</td><td style = "text-align: right;">330.077</td></tr></tbody></table></div>

```julia
# 別解
df_receipt |>
    @groupby(_.store_cd) |>
    @map({store_cd=key(_), mean_amount=mean(_.amount)}) |>
    @filter(_.mean_amount >= 330) |>
    @orderby(_.store_cd) |>
    DataFrame
```

<div><div style = "float: left;"><span>13×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">mean_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String7" style = "text-align: left;">String7</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">S12013</td><td style = "text-align: right;">330.194</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">S13001</td><td style = "text-align: right;">348.47</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">S13003</td><td style = "text-align: right;">350.916</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">S13004</td><td style = "text-align: right;">330.944</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">S13015</td><td style = "text-align: right;">351.112</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">S13019</td><td style = "text-align: right;">330.209</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">S13020</td><td style = "text-align: right;">337.88</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">S13052</td><td style = "text-align: right;">402.867</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">S14010</td><td style = "text-align: right;">348.791</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">S14011</td><td style = "text-align: right;">335.718</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">11</td><td style = "text-align: left;">S14026</td><td style = "text-align: right;">332.341</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">12</td><td style = "text-align: left;">S14045</td><td style = "text-align: right;">330.082</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">13</td><td style = "text-align: left;">S14047</td><td style = "text-align: right;">330.077</td></tr></tbody></table></div>

## 18.　グループ化されたデータフレームごとの集計結果との比較

---
> J-034: レシート明細データ（df_receipt）に対し、顧客ID（customer_id）ごとに売上金額（amount）を合計して全顧客の平均を求めよ。ただし、顧客IDが"Z"から始まるものは非会員を表すため、除外して計算すること。

```julia
using StatsBase

df2 = df_receipt |>
    @filter(!occursin(r"^Z", _.customer_id)) |>
    DataFrame;

gd = groupby(df2, :customer_id);

df3 = combine(gd, :amount => sum => :sum_amount)

using StatsBase
mean(df3.sum_amount)
```

    2547.742234529256

```julia
# 別解
df2 = df_receipt |>
    @filter(!occursin(r"^Z", _.customer_id)) |>
    @groupby(_.customer_id) |>
    @map({sum=sum(_.amount)}) |>
    DataFrame |>
    x -> mean(x.sum)
```

    2547.742234529256

---
> J-035: レシート明細データ（df_receipt）に対し、顧客ID（customer_id）ごとに売上金額（amount）を合計して全顧客の平均を求め、平均以上に買い物をしている顧客を抽出し、10件表示せよ。ただし、顧客IDが"Z"から始まるものは非会員を表すため、除外して計算すること。

```julia
df2 = df_receipt |>
    @filter(!occursin(r"^Z", _.customer_id)) |>
    DataFrame;

gd = groupby(df2, :customer_id);

df3 = combine(gd, :amount => sum => :sum_amount)

using StatsBase
mean_amount = mean(df3.sum_amount)
```

    2547.742234529256

```julia
df3 |>
    @filter(_.sum_amount > mean_amount) |>
    @orderby(_.customer_id) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sum_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">3044</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">3337</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001214000009</td><td style = "text-align: right;">4685</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001214000017</td><td style = "text-align: right;">4132</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001214000052</td><td style = "text-align: right;">5639</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001215000040</td><td style = "text-align: right;">3496</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001304000006</td><td style = "text-align: right;">3726</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001305000005</td><td style = "text-align: right;">3485</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001305000011</td><td style = "text-align: right;">4370</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001315000180</td><td style = "text-align: right;">3300</td></tr></tbody></table></div>

```julia
# 別解
df2 = df_receipt |>
    @filter(!occursin(r"^Z", _.customer_id)) |>
    @groupby(_.customer_id) |>
    @map({customer_id=_.customer_id[1], sum_amount=sum(_.amount)}) |>
    DataFrame

grandmean = mean(df2.sum_amount)

df2 |>
    @filter(_.sum_amount > grandmean) |>
    @orderby(_.customer_id) |>
    @take(10) |>
    DataFrame

```

<div><div style = "float: left;"><span>10×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sum_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">3044</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">3337</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001214000009</td><td style = "text-align: right;">4685</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001214000017</td><td style = "text-align: right;">4132</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001214000052</td><td style = "text-align: right;">5639</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001215000040</td><td style = "text-align: right;">3496</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001304000006</td><td style = "text-align: right;">3726</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001305000005</td><td style = "text-align: right;">3485</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001305000011</td><td style = "text-align: right;">4370</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001315000180</td><td style = "text-align: right;">3300</td></tr></tbody></table></div>

# データフレームの結合(join)

## 19.　`innnerjoin()`

---
> J-036: レシート明細データ（df_receipt）と店舗データ（df_store）を内部結合し、レシート明細データの全項目と店舗データの店舗名（store_name）を10件表示せよ。

```julia
innerjoin(df_receipt, df_store[!, [:store_cd, :store_name]], on=:store_cd) |> (x -> first(x, 10))
```

<div><div style = "float: left;"><span>10×10 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">sales_epoch</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">receipt_no</th><th style = "text-align: left;">receipt_sub_no</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">quantity</th><th style = "text-align: left;">amount</th><th style = "text-align: left;">store_name</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String31" style = "text-align: left;">String31</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20181103</td><td style = "text-align: right;">1541203200</td><td style = "text-align: left;">S14006</td><td style = "text-align: right;">112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS006214000001</td><td style = "text-align: left;">P070305012</td><td style = "text-align: right;">1</td><td style = "text-align: right;">158</td><td style = "text-align: left;">葛が谷店</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20181118</td><td style = "text-align: right;">1542499200</td><td style = "text-align: left;">S13008</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">2</td><td style = "text-align: left;">CS008415000097</td><td style = "text-align: left;">P070701017</td><td style = "text-align: right;">1</td><td style = "text-align: right;">81</td><td style = "text-align: left;">成城店</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170712</td><td style = "text-align: right;">1499817600</td><td style = "text-align: left;">S14028</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS028414000014</td><td style = "text-align: left;">P060101005</td><td style = "text-align: right;">1</td><td style = "text-align: right;">170</td><td style = "text-align: left;">二ツ橋店</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20190205</td><td style = "text-align: right;">1549324800</td><td style = "text-align: left;">S14042</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">1</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: left;">P050301001</td><td style = "text-align: right;">1</td><td style = "text-align: right;">25</td><td style = "text-align: left;">新山下店</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20180821</td><td style = "text-align: right;">1534809600</td><td style = "text-align: left;">S14025</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">CS025415000050</td><td style = "text-align: left;">P060102007</td><td style = "text-align: right;">1</td><td style = "text-align: right;">90</td><td style = "text-align: left;">大和店</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">20190605</td><td style = "text-align: right;">1559692800</td><td style = "text-align: left;">S13003</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS003515000195</td><td style = "text-align: left;">P050102002</td><td style = "text-align: right;">1</td><td style = "text-align: right;">138</td><td style = "text-align: left;">狛江店</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">20181205</td><td style = "text-align: right;">1543968000</td><td style = "text-align: left;">S14024</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">CS024514000042</td><td style = "text-align: left;">P080101005</td><td style = "text-align: right;">1</td><td style = "text-align: right;">30</td><td style = "text-align: left;">三田店</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">20190922</td><td style = "text-align: right;">1569110400</td><td style = "text-align: left;">S14040</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS040415000178</td><td style = "text-align: left;">P070501004</td><td style = "text-align: right;">1</td><td style = "text-align: right;">128</td><td style = "text-align: left;">長津田店</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">20170504</td><td style = "text-align: right;">1493856000</td><td style = "text-align: left;">S13020</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">2</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: left;">P071302010</td><td style = "text-align: right;">1</td><td style = "text-align: right;">770</td><td style = "text-align: left;">十条仲原店</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">20191010</td><td style = "text-align: right;">1570665600</td><td style = "text-align: left;">S14027</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS027514000015</td><td style = "text-align: left;">P071101003</td><td style = "text-align: right;">1</td><td style = "text-align: right;">680</td><td style = "text-align: left;">南藤沢店</td></tr></tbody></table></div>

```julia
# 別解
df_receipt |>
    @join(df_store, _.store_cd, _.store_cd,
        {_.sales_ymd, _.sales_epoch, _.store_cd, _.receipt_no,
        _.receipt_sub_no, _.customer_id, _.product_cd, _.quantity,
        _.amount, __.store_name}) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×10 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">sales_epoch</th><th style = "text-align: left;">store_cd</th><th style = "text-align: left;">receipt_no</th><th style = "text-align: left;">receipt_sub_no</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">quantity</th><th style = "text-align: left;">amount</th><th style = "text-align: left;">store_name</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String31" style = "text-align: left;">String31</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20181103</td><td style = "text-align: right;">1541203200</td><td style = "text-align: left;">S14006</td><td style = "text-align: right;">112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS006214000001</td><td style = "text-align: left;">P070305012</td><td style = "text-align: right;">1</td><td style = "text-align: right;">158</td><td style = "text-align: left;">葛が谷店</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20181118</td><td style = "text-align: right;">1542499200</td><td style = "text-align: left;">S13008</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">2</td><td style = "text-align: left;">CS008415000097</td><td style = "text-align: left;">P070701017</td><td style = "text-align: right;">1</td><td style = "text-align: right;">81</td><td style = "text-align: left;">成城店</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170712</td><td style = "text-align: right;">1499817600</td><td style = "text-align: left;">S14028</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS028414000014</td><td style = "text-align: left;">P060101005</td><td style = "text-align: right;">1</td><td style = "text-align: right;">170</td><td style = "text-align: left;">二ツ橋店</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20190205</td><td style = "text-align: right;">1549324800</td><td style = "text-align: left;">S14042</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">1</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: left;">P050301001</td><td style = "text-align: right;">1</td><td style = "text-align: right;">25</td><td style = "text-align: left;">新山下店</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20180821</td><td style = "text-align: right;">1534809600</td><td style = "text-align: left;">S14025</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">CS025415000050</td><td style = "text-align: left;">P060102007</td><td style = "text-align: right;">1</td><td style = "text-align: right;">90</td><td style = "text-align: left;">大和店</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">20190605</td><td style = "text-align: right;">1559692800</td><td style = "text-align: left;">S13003</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS003515000195</td><td style = "text-align: left;">P050102002</td><td style = "text-align: right;">1</td><td style = "text-align: right;">138</td><td style = "text-align: left;">狛江店</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">20181205</td><td style = "text-align: right;">1543968000</td><td style = "text-align: left;">S14024</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">CS024514000042</td><td style = "text-align: left;">P080101005</td><td style = "text-align: right;">1</td><td style = "text-align: right;">30</td><td style = "text-align: left;">三田店</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">20190922</td><td style = "text-align: right;">1569110400</td><td style = "text-align: left;">S14040</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS040415000178</td><td style = "text-align: left;">P070501004</td><td style = "text-align: right;">1</td><td style = "text-align: right;">128</td><td style = "text-align: left;">長津田店</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">20170504</td><td style = "text-align: right;">1493856000</td><td style = "text-align: left;">S13020</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">2</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: left;">P071302010</td><td style = "text-align: right;">1</td><td style = "text-align: right;">770</td><td style = "text-align: left;">十条仲原店</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">20191010</td><td style = "text-align: right;">1570665600</td><td style = "text-align: left;">S14027</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">CS027514000015</td><td style = "text-align: left;">P071101003</td><td style = "text-align: right;">1</td><td style = "text-align: right;">680</td><td style = "text-align: left;">南藤沢店</td></tr></tbody></table></div>

---
> J-037: 商品データ（df_product）とカテゴリデータ（df_category）を内部結合し、商品データの全項目とカテゴリデータのカテゴリ小区分名（category_small_name）を10件表示せよ。

```julia
first(innerjoin(df_product, df_category[!, [:category_small_name, :category_small_cd]], on=:category_small_cd, makeunique=true), 10)
```

<div><div style = "float: left;"><span>10×7 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">category_major_cd</th><th style = "text-align: left;">category_medium_cd</th><th style = "text-align: left;">category_small_cd</th><th style = "text-align: left;">unit_price</th><th style = "text-align: left;">unit_cost</th><th style = "text-align: left;">category_small_name</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String" style = "text-align: left;">String</th><th title = "String" style = "text-align: left;">String</th><th title = "Union{Missing, Int64}" style = "text-align: left;">Int64?</th><th title = "Union{Missing, Int64}" style = "text-align: left;">Int64?</th><th title = "String" style = "text-align: left;">String</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">P040101001</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">198</td><td style = "text-align: right;">149</td><td style = "text-align: left;">弁当類</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">P040101002</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">218</td><td style = "text-align: right;">164</td><td style = "text-align: left;">弁当類</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">P040101003</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">230</td><td style = "text-align: right;">173</td><td style = "text-align: left;">弁当類</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">P040101004</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">248</td><td style = "text-align: right;">186</td><td style = "text-align: left;">弁当類</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">P040101005</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">268</td><td style = "text-align: right;">201</td><td style = "text-align: left;">弁当類</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">P040101006</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">298</td><td style = "text-align: right;">224</td><td style = "text-align: left;">弁当類</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">P040101007</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">338</td><td style = "text-align: right;">254</td><td style = "text-align: left;">弁当類</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">P040101008</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">420</td><td style = "text-align: right;">315</td><td style = "text-align: left;">弁当類</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">P040101009</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">498</td><td style = "text-align: right;">374</td><td style = "text-align: left;">弁当類</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">P040101010</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">580</td><td style = "text-align: right;">435</td><td style = "text-align: left;">弁当類</td></tr></tbody></table></div>

```julia
# 別解
df_product |>
    @join(df_category, _.category_small_cd, _.category_small_cd,
        {_.product_cd, _.category_major_cd, _.category_medium_cd,
        _.category_small_cd, _.unit_price, _.unit_cost,
        __.category_small_name}) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×7 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">category_major_cd</th><th style = "text-align: left;">category_medium_cd</th><th style = "text-align: left;">category_small_cd</th><th style = "text-align: left;">unit_price</th><th style = "text-align: left;">unit_cost</th><th style = "text-align: left;">category_small_name</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String" style = "text-align: left;">String</th><th title = "String" style = "text-align: left;">String</th><th title = "Union{Missing, Int64}" style = "text-align: left;">Int64?</th><th title = "Union{Missing, Int64}" style = "text-align: left;">Int64?</th><th title = "String" style = "text-align: left;">String</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">P040101001</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">198</td><td style = "text-align: right;">149</td><td style = "text-align: left;">弁当類</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">P040101002</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">218</td><td style = "text-align: right;">164</td><td style = "text-align: left;">弁当類</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">P040101003</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">230</td><td style = "text-align: right;">173</td><td style = "text-align: left;">弁当類</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">P040101004</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">248</td><td style = "text-align: right;">186</td><td style = "text-align: left;">弁当類</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">P040101005</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">268</td><td style = "text-align: right;">201</td><td style = "text-align: left;">弁当類</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">P040101006</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">298</td><td style = "text-align: right;">224</td><td style = "text-align: left;">弁当類</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">P040101007</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">338</td><td style = "text-align: right;">254</td><td style = "text-align: left;">弁当類</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">P040101008</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">420</td><td style = "text-align: right;">315</td><td style = "text-align: left;">弁当類</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">P040101009</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">498</td><td style = "text-align: right;">374</td><td style = "text-align: left;">弁当類</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">P040101010</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">580</td><td style = "text-align: right;">435</td><td style = "text-align: left;">弁当類</td></tr></tbody></table></div>

## 20.　`leftjoin()`

---
> J-038: 顧客データ（df_customer）とレシート明細データ（df_receipt）から、顧客ごとの売上金額合計を求め、10件表示せよ。ただし、売上実績がない顧客については売上金額を0として表示させること。また、顧客は性別コード（gender_cd）が女性（1）であるものを対象とし、非会員（顧客IDが"Z"から始まるもの）は除外すること。

```julia
df = leftjoin(df_customer[!, [:customer_id, :gender_cd]], df_receipt[!, [:customer_id, :amount]], on=:customer_id)
filter!(:customer_id => x -> !occursin(r"^Z", x), df)
filter!(:gender_cd => x -> x == "1", df);
df2 = groupby(df, :customer_id)
df3 = combine(df2, :amount => sum => :sum_amount);
df3.sum_amount .= coalesce.(df3.sum_amount, 0);  # coalesce() は missing の置換に使える
first(sort(df3, :customer_id), 10)
```

<div><div style = "float: left;"><span>10×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sum_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001112000009</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001112000019</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001112000021</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001112000023</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001112000024</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001112000029</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001112000030</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: right;">1298</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001113000010</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">626</td></tr></tbody></table></div>

```julia
# 別解
leftjoin(df_customer[!, [:customer_id, :gender_cd]], df_receipt[!, [:customer_id, :amount]], on=:customer_id) |>
    @filter(!occursin(r"^Z", _.customer_id)) |>
    @filter(_.gender_cd == "1") |>
    @groupby(_.customer_id) |>
    @map({customer_id=key(_), sum_amount=sum(_.amount)}) |>
    @replacena(:sum_amount => 0) |>
    @orderby(_.customer_id) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sum_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001112000009</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001112000019</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001112000021</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001112000023</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001112000024</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001112000029</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001112000030</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: right;">1298</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001113000010</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">626</td></tr></tbody></table></div>

## 21.　`outerjoin()`

---
> J-039: レシート明細データ（df_receipt）から、売上日数の多い顧客の上位20件を抽出したデータと、売上金額合計の多い顧客の上位20件を抽出したデータをそれぞれ作成し、さらにその2つを完全外部結合せよ。ただし、非会員（顧客IDが"Z"から始まるもの）は除外すること。

```julia
df2 = groupby(filter(:customer_id => x -> !occursin(r"^Z", x), df_receipt), :customer_id)
df3 = sort(combine(df2, :sales_ymd => (x -> length(unique(x))) => :come_days), :come_days, rev=true)[1:20, :];
df4 = sort(combine(df2, :amount => sum => :sum_amount), :sum_amount, rev=true)[1:20, :];

io = IOContext(stdout, :limit => false)
println(outerjoin(df3, df4, on=:customer_id, makeunique=true))
io = IOContext(stdout, :limit => true)
```

    [1m34×3 DataFrame[0m
    [1m Row [0m│[1m customer_id    [0m[1m come_days [0m[1m sum_amount [0m
         │[90m String15       [0m[90m Int64?    [0m[90m Int64?     [0m
    ─────┼───────────────────────────────────────
       1 │ CS017415000097         20       23086
       2 │ CS015415000185         22       20153
       3 │ CS031414000051         19       19202
       4 │ CS028415000007         21       19127
       5 │ CS010214000010         22       18585
       6 │ CS016415000141         20       18372
       7 │ CS040214000008         23 [90m    missing [0m
       8 │ CS010214000002         21 [90m    missing [0m
       9 │ CS039414000052         19 [90m    missing [0m
      10 │ CS021514000045         19 [90m    missing [0m
      11 │ CS022515000226         19 [90m    missing [0m
      12 │ CS014214000023         19 [90m    missing [0m
      13 │ CS021515000172         19 [90m    missing [0m
      14 │ CS014415000077         18 [90m    missing [0m
      15 │ CS030214000008         18 [90m    missing [0m
      16 │ CS021515000056         18 [90m    missing [0m
      17 │ CS031414000073         18 [90m    missing [0m
      18 │ CS032415000209         18 [90m    missing [0m
      19 │ CS007515000107         18 [90m    missing [0m
      20 │ CS021515000211         18 [90m    missing [0m
      21 │ CS001605000009 [90m   missing [0m      18925
      22 │ CS006515000023 [90m   missing [0m      18372
      23 │ CS011414000106 [90m   missing [0m      18338
      24 │ CS038415000104 [90m   missing [0m      17847
      25 │ CS035414000024 [90m   missing [0m      17615
      26 │ CS021515000089 [90m   missing [0m      17580
      27 │ CS032414000072 [90m   missing [0m      16563
      28 │ CS016415000101 [90m   missing [0m      16348
      29 │ CS011415000006 [90m   missing [0m      16094
      30 │ CS034415000047 [90m   missing [0m      16083
      31 │ CS007514000094 [90m   missing [0m      15735
      32 │ CS009414000059 [90m   missing [0m      15492
      33 │ CS030415000034 [90m   missing [0m      15468
      34 │ CS015515000034 [90m   missing [0m      15300

    IOContext(Base.PipeEndpoint(RawFD(40) open, 0 bytes waiting))

```julia
# 別解
df2 = df_receipt |>
    @filter(!occursin(r"^Z", _.customer_id)) |>
    DataFrame
gd = groupby(df2, :customer_id)
df3 = combine(gd, :sales_ymd => (x -> length(unique(x))) => :come_days) |>
    @orderby_descending(_.come_days) |>
    @take(20) |>
    DataFrame
df4 = combine(gd, :amount => sum) |>
    @orderby_descending(_.amount_sum) |>
    @take(20) |>
    DataFrame;
```

```julia
io = IOContext(stdout, :limit => false)
println(outerjoin(df3, df4, on=:customer_id))
io = IOContext(stdout, :limit => true)
```

    [1m34×3 DataFrame[0m
    [1m Row [0m│[1m customer_id    [0m[1m come_days [0m[1m amount_sum [0m
         │[90m String15       [0m[90m Int64?    [0m[90m Int64?     [0m
    ─────┼───────────────────────────────────────
       1 │ CS017415000097         20       23086
       2 │ CS015415000185         22       20153
       3 │ CS031414000051         19       19202
       4 │ CS028415000007         21       19127
       5 │ CS010214000010         22       18585
       6 │ CS016415000141         20       18372
       7 │ CS040214000008         23 [90m    missing [0m
       8 │ CS010214000002         21 [90m    missing [0m
       9 │ CS039414000052         19 [90m    missing [0m
      10 │ CS021514000045         19 [90m    missing [0m
      11 │ CS022515000226         19 [90m    missing [0m
      12 │ CS014214000023         19 [90m    missing [0m
      13 │ CS021515000172         19 [90m    missing [0m
      14 │ CS014415000077         18 [90m    missing [0m
      15 │ CS030214000008         18 [90m    missing [0m
      16 │ CS021515000056         18 [90m    missing [0m
      17 │ CS031414000073         18 [90m    missing [0m
      18 │ CS032415000209         18 [90m    missing [0m
      19 │ CS007515000107         18 [90m    missing [0m
      20 │ CS021515000211         18 [90m    missing [0m
      21 │ CS001605000009 [90m   missing [0m      18925
      22 │ CS006515000023 [90m   missing [0m      18372
      23 │ CS011414000106 [90m   missing [0m      18338
      24 │ CS038415000104 [90m   missing [0m      17847
      25 │ CS035414000024 [90m   missing [0m      17615
      26 │ CS021515000089 [90m   missing [0m      17580
      27 │ CS032414000072 [90m   missing [0m      16563
      28 │ CS016415000101 [90m   missing [0m      16348
      29 │ CS011415000006 [90m   missing [0m      16094
      30 │ CS034415000047 [90m   missing [0m      16083
      31 │ CS007514000094 [90m   missing [0m      15735
      32 │ CS009414000059 [90m   missing [0m      15492
      33 │ CS030415000034 [90m   missing [0m      15468
      34 │ CS015515000034 [90m   missing [0m      15300

    IOContext(Base.PipeEndpoint(RawFD(40) open, 0 bytes waiting))

---
> J-040: 全ての店舗と全ての商品を組み合わせたデータを作成したい。店舗データ（df_store）と商品データ（df_product）を直積し、件数を計算せよ。

件数を計算するだけなら，それぞれの件数の積である。

```julia
nrow(df_store) * nrow(df_product)
```

    531590

```julia
# 別解
df_store2 = copy(df_store);
insertcols!(df_store2, 1, :key => 0);

df_product2 = copy(df_product);
insertcols!(df_product2, 1, :key => 0);

outerjoin(df_store2, df_product2, on=:key) |>
    nrow
```

    531590

# 差分

---
> J-041: レシート明細データ（df_receipt）の売上金額（amount）を日付（sales_ymd）ごとに集計し、前回売上があった日からの売上金額増減を計算せよ。そして結果を10件表示せよ。

```julia
gd = groupby(df_receipt, :sales_ymd)
df = combine(gd, :amount => sum) |>
    @orderby(_.sales_ymd) |>
    DataFrame;
df2 = df[1:end-1, :] |>
    @rename(:sales_ymd => :lag_sales_ymd, :amount_sum => :lag_amount_sum) |>
    DataFrame;
```

```julia
df4 = vcat(DataFrame(lag_sales_ymd = missing, lag_amount_sum = missing), df2);
```

```julia
hcat(df, df4) |>
    @mutate(diff_amount = _.amount_sum - _.lag_amount_sum) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×5 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">amount_sum</th><th style = "text-align: left;">lag_sales_ymd</th><th style = "text-align: left;">lag_amount_sum</th><th style = "text-align: left;">diff_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Union{Missing, Int64}" style = "text-align: left;">Int64?</th><th title = "Union{Missing, Int64}" style = "text-align: left;">Int64?</th><th title = "Union{Missing, Int64}" style = "text-align: left;">Int64?</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20170101</td><td style = "text-align: right;">33723</td><td style = "font-style: italic; text-align: right;">missing</td><td style = "font-style: italic; text-align: right;">missing</td><td style = "font-style: italic; text-align: right;">missing</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20170102</td><td style = "text-align: right;">24165</td><td style = "text-align: right;">20170101</td><td style = "text-align: right;">33723</td><td style = "text-align: right;">-9558</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170103</td><td style = "text-align: right;">27503</td><td style = "text-align: right;">20170102</td><td style = "text-align: right;">24165</td><td style = "text-align: right;">3338</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20170104</td><td style = "text-align: right;">36165</td><td style = "text-align: right;">20170103</td><td style = "text-align: right;">27503</td><td style = "text-align: right;">8662</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20170105</td><td style = "text-align: right;">37830</td><td style = "text-align: right;">20170104</td><td style = "text-align: right;">36165</td><td style = "text-align: right;">1665</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">20170106</td><td style = "text-align: right;">32387</td><td style = "text-align: right;">20170105</td><td style = "text-align: right;">37830</td><td style = "text-align: right;">-5443</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">20170107</td><td style = "text-align: right;">23415</td><td style = "text-align: right;">20170106</td><td style = "text-align: right;">32387</td><td style = "text-align: right;">-8972</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">20170108</td><td style = "text-align: right;">24737</td><td style = "text-align: right;">20170107</td><td style = "text-align: right;">23415</td><td style = "text-align: right;">1322</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">20170109</td><td style = "text-align: right;">26718</td><td style = "text-align: right;">20170108</td><td style = "text-align: right;">24737</td><td style = "text-align: right;">1981</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">20170110</td><td style = "text-align: right;">20143</td><td style = "text-align: right;">20170109</td><td style = "text-align: right;">26718</td><td style = "text-align: right;">-6575</td></tr></tbody></table></div>

```julia
# 別解
gd = groupby(df_receipt, :sales_ymd);
df = combine(gd, :amount => sum) |>
    @orderby(_.sales_ymd) |>
    DataFrame
d = Vector{Union{Int64, Missing}}(missing, nrow(df))
d[2:end] = diff(df[:, :amount_sum]);
DataFrame(sales_ymd = df.sales_ymd, diff_amount = d) |>
    @take(10)
```

<table><thead><tr><th>sales_ymd</th><th>diff_amount</th></tr></thead><tbody><tr><td>20170101</td><td>#NA</td></tr><tr><td>20170102</td><td>-9558</td></tr><tr><td>20170103</td><td>3338</td></tr><tr><td>20170104</td><td>8662</td></tr><tr><td>20170105</td><td>1665</td></tr><tr><td>20170106</td><td>-5443</td></tr><tr><td>20170107</td><td>-8972</td></tr><tr><td>20170108</td><td>1322</td></tr><tr><td>20170109</td><td>1981</td></tr><tr><td>20170110</td><td>-6575</td></tr></tbody></table>

1 行目（amount_sum が missing）が必要なければ，以下よりもさらに簡単になる。

```julia
# 別解2
gd = groupby(df_receipt, :sales_ymd);
df = combine(gd, :amount => sum) |>
    @orderby(_.sales_ymd) |>
    DataFrame |>
    allowmissing!;  # これをしないと missing を代入できない
df[2:end, :amount_sum] = diff(df[:, :amount_sum]);
df[1, :amount_sum] = missing  # 代入の順番も考慮する
df |>
    @rename(:amount_sum => :diff_amount) |>
    @take(10) 
```

<table><thead><tr><th>sales_ymd</th><th>diff_amount</th></tr></thead><tbody><tr><td>20170101</td><td>#NA</td></tr><tr><td>20170102</td><td>-9558</td></tr><tr><td>20170103</td><td>3338</td></tr><tr><td>20170104</td><td>8662</td></tr><tr><td>20170105</td><td>1665</td></tr><tr><td>20170106</td><td>-5443</td></tr><tr><td>20170107</td><td>-8972</td></tr><tr><td>20170108</td><td>1322</td></tr><tr><td>20170109</td><td>1981</td></tr><tr><td>20170110</td><td>-6575</td></tr></tbody></table>

```julia
# 別解3
gd = groupby(df_receipt, :sales_ymd);
df = combine(gd, :amount => sum) |>
    @orderby(_.sales_ymd) |>
    DataFrame;
DataFrame(sales_ymd = df[2:end, 1], diff_amount = diff(df[:, :amount_sum])) |>
    @take(10)
```

<table><thead><tr><th>sales_ymd</th><th>diff_amount</th></tr></thead><tbody><tr><td>20170102</td><td>-9558</td></tr><tr><td>20170103</td><td>3338</td></tr><tr><td>20170104</td><td>8662</td></tr><tr><td>20170105</td><td>1665</td></tr><tr><td>20170106</td><td>-5443</td></tr><tr><td>20170107</td><td>-8972</td></tr><tr><td>20170108</td><td>1322</td></tr><tr><td>20170109</td><td>1981</td></tr><tr><td>20170110</td><td>-6575</td></tr><tr><td>20170111</td><td>4144</td></tr></tbody></table>

---
> J-042: レシート明細データ（df_receipt）の売上金額（amount）を日付（sales_ymd）ごとに集計し、各日付のデータに対し、前回、前々回、3回前に売上があった日のデータを結合せよ。そして結果を10件表示せよ。

```julia
gd = groupby(df_receipt, :sales_ymd);
df = combine(gd, :amount => sum) |>
    @orderby(_.sales_ymd) |>
    DataFrame;

df2 = DataFrame(sales_ymd = 0, amount = 0, lag_ymd = 0, lag_amount = 0)
for i = 2:nrow(df)
    i >= 4 && append!(df2, DataFrame(sales_ymd = df[i, 1], amount = df[i, 2], lag_ymd = df[i-3, 1], lag_amount = df[i-3, 2]))
    i >= 3 && append!(df2, DataFrame(sales_ymd = df[i, 1], amount = df[i, 2], lag_ymd = df[i-2, 1], lag_amount = df[i-2, 2]))
              append!(df2, DataFrame(sales_ymd = df[i, 1], amount = df[i, 2], lag_ymd = df[i-1, 1], lag_amount = df[i-1, 2]))
end
delete!(df2, 1)
first(df2, 10)
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">amount</th><th style = "text-align: left;">lag_ymd</th><th style = "text-align: left;">lag_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20170102</td><td style = "text-align: right;">24165</td><td style = "text-align: right;">20170101</td><td style = "text-align: right;">33723</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20170103</td><td style = "text-align: right;">27503</td><td style = "text-align: right;">20170101</td><td style = "text-align: right;">33723</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170103</td><td style = "text-align: right;">27503</td><td style = "text-align: right;">20170102</td><td style = "text-align: right;">24165</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20170104</td><td style = "text-align: right;">36165</td><td style = "text-align: right;">20170101</td><td style = "text-align: right;">33723</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20170104</td><td style = "text-align: right;">36165</td><td style = "text-align: right;">20170102</td><td style = "text-align: right;">24165</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">20170104</td><td style = "text-align: right;">36165</td><td style = "text-align: right;">20170103</td><td style = "text-align: right;">27503</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">20170105</td><td style = "text-align: right;">37830</td><td style = "text-align: right;">20170102</td><td style = "text-align: right;">24165</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">20170105</td><td style = "text-align: right;">37830</td><td style = "text-align: right;">20170103</td><td style = "text-align: right;">27503</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">20170105</td><td style = "text-align: right;">37830</td><td style = "text-align: right;">20170104</td><td style = "text-align: right;">36165</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">20170106</td><td style = "text-align: right;">32387</td><td style = "text-align: right;">20170103</td><td style = "text-align: right;">27503</td></tr></tbody></table></div>

```julia
# 別解
gd = groupby(df_receipt, :sales_ymd);
df = combine(gd, :amount => sum) |>
    @orderby(_.sales_ymd) |>
    DataFrame;

df2 = DataFrame(sales_ymd = 0, amount = 0,
        lag_ymd_1 = 0, lag_amount_1 = 0,
        lag_ymd_2 = 0, lag_amount_2 = 0,
        lag_ymd_3 = 0, lag_amount_3 = 0)
for i = 4:nrow(df)
    append!(df2,
        DataFrame(sales_ymd = df[i, 1], amount = df[i, 2],
            lag_ymd_1 = df[i-1, 1], lag_amount_1 = df[i-1, 2],
            lag_ymd_2 = df[i-2, 1], lag_amount_2 = df[i-2, 2],
            lag_ymd_3 = df[i-3, 1], lag_amount_3 = df[i-3, 2]
            ))
end
delete!(df2, 1)
first(df2, 10)
```

<div><div style = "float: left;"><span>10×8 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">amount</th><th style = "text-align: left;">lag_ymd_1</th><th style = "text-align: left;">lag_amount_1</th><th style = "text-align: left;">lag_ymd_2</th><th style = "text-align: left;">lag_amount_2</th><th style = "text-align: left;">lag_ymd_3</th><th style = "text-align: left;">lag_amount_3</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20170104</td><td style = "text-align: right;">36165</td><td style = "text-align: right;">20170103</td><td style = "text-align: right;">27503</td><td style = "text-align: right;">20170102</td><td style = "text-align: right;">24165</td><td style = "text-align: right;">20170101</td><td style = "text-align: right;">33723</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20170105</td><td style = "text-align: right;">37830</td><td style = "text-align: right;">20170104</td><td style = "text-align: right;">36165</td><td style = "text-align: right;">20170103</td><td style = "text-align: right;">27503</td><td style = "text-align: right;">20170102</td><td style = "text-align: right;">24165</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170106</td><td style = "text-align: right;">32387</td><td style = "text-align: right;">20170105</td><td style = "text-align: right;">37830</td><td style = "text-align: right;">20170104</td><td style = "text-align: right;">36165</td><td style = "text-align: right;">20170103</td><td style = "text-align: right;">27503</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20170107</td><td style = "text-align: right;">23415</td><td style = "text-align: right;">20170106</td><td style = "text-align: right;">32387</td><td style = "text-align: right;">20170105</td><td style = "text-align: right;">37830</td><td style = "text-align: right;">20170104</td><td style = "text-align: right;">36165</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20170108</td><td style = "text-align: right;">24737</td><td style = "text-align: right;">20170107</td><td style = "text-align: right;">23415</td><td style = "text-align: right;">20170106</td><td style = "text-align: right;">32387</td><td style = "text-align: right;">20170105</td><td style = "text-align: right;">37830</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">20170109</td><td style = "text-align: right;">26718</td><td style = "text-align: right;">20170108</td><td style = "text-align: right;">24737</td><td style = "text-align: right;">20170107</td><td style = "text-align: right;">23415</td><td style = "text-align: right;">20170106</td><td style = "text-align: right;">32387</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">20170110</td><td style = "text-align: right;">20143</td><td style = "text-align: right;">20170109</td><td style = "text-align: right;">26718</td><td style = "text-align: right;">20170108</td><td style = "text-align: right;">24737</td><td style = "text-align: right;">20170107</td><td style = "text-align: right;">23415</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">20170111</td><td style = "text-align: right;">24287</td><td style = "text-align: right;">20170110</td><td style = "text-align: right;">20143</td><td style = "text-align: right;">20170109</td><td style = "text-align: right;">26718</td><td style = "text-align: right;">20170108</td><td style = "text-align: right;">24737</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">20170112</td><td style = "text-align: right;">23526</td><td style = "text-align: right;">20170111</td><td style = "text-align: right;">24287</td><td style = "text-align: right;">20170110</td><td style = "text-align: right;">20143</td><td style = "text-align: right;">20170109</td><td style = "text-align: right;">26718</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">20170113</td><td style = "text-align: right;">28004</td><td style = "text-align: right;">20170112</td><td style = "text-align: right;">23526</td><td style = "text-align: right;">20170111</td><td style = "text-align: right;">24287</td><td style = "text-align: right;">20170110</td><td style = "text-align: right;">20143</td></tr></tbody></table></div>

# クロス集計

---
> J-043： レシート明細データ（df_receipt）と顧客データ（df_customer）を結合し、性別コード（gender_cd）と年代（ageから計算）ごとに売上金額（amount）を合計した売上サマリデータを作成せよ。性別コードは0が男性、1が女性、9が不明を表すものとする。
>
> ただし、項目構成は年代、女性の売上金額、男性の売上金額、性別不明の売上金額の4項目とすること（縦に年代、横に性別のクロス集計）。また、年代は10歳ごとの階級とすること。

```julia
df = innerjoin(df_receipt[!, [:customer_id, :amount]],
        df_customer[!, [:customer_id, :gender_cd, :age]],
        on=:customer_id, makeunique=true)
df.age = div.(df.age, 10) .* 10
gd = groupby(df, [:age, :gender_cd])
df2 = combine(gd, :amount => sum);
using NamedArrays
using FreqTables
table = freqtable(df2.age, df2.gender_cd, weights=df2.amount_sum)
df043 = hcat(10:10:90, DataFrame(Matrix(table), :auto), makeunique=true)
rename!(df043, [:era, :male, :female, :unknown])
```

<div><div style = "float: left;"><span>9×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">era</th><th style = "text-align: left;">male</th><th style = "text-align: left;">female</th><th style = "text-align: left;">unknown</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">10</td><td style = "text-align: right;">1591</td><td style = "text-align: right;">149836</td><td style = "text-align: right;">4317</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20</td><td style = "text-align: right;">72940</td><td style = "text-align: right;">1363724</td><td style = "text-align: right;">44328</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">30</td><td style = "text-align: right;">177322</td><td style = "text-align: right;">693047</td><td style = "text-align: right;">50441</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">40</td><td style = "text-align: right;">19355</td><td style = "text-align: right;">9320791</td><td style = "text-align: right;">483512</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">50</td><td style = "text-align: right;">54320</td><td style = "text-align: right;">6685192</td><td style = "text-align: right;">342923</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">60</td><td style = "text-align: right;">272469</td><td style = "text-align: right;">987741</td><td style = "text-align: right;">71418</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">70</td><td style = "text-align: right;">13435</td><td style = "text-align: right;">29764</td><td style = "text-align: right;">2427</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">80</td><td style = "text-align: right;">46360</td><td style = "text-align: right;">262923</td><td style = "text-align: right;">5111</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">90</td><td style = "text-align: right;">0</td><td style = "text-align: right;">6260</td><td style = "text-align: right;">0</td></tr></tbody></table></div>

# 縦長フォーマット

---
> J-044： 043で作成した売上サマリデータ（df_sales_summary）は性別の売上を横持ちさせたものであった。このデータから性別を縦持ちさせ、年代、性別コード、売上金額の3項目に変換せよ。ただし、性別コードは男性を"00"、女性を"01"、不明を"99"とする。

```julia
df2 = stack(df043, 2:4)
df2[df2[:, 2] .== "male", 2] .= "00"
df2[df2[:, 2] .== "female", 2] .= "01"
df2[df2[:, 2] .== "unknown", 2] .= "99";
```

```julia
df2
```

<div><div style = "float: left;"><span>27×3 DataFrame</span></div><div style = "float: right;"><span style = "font-style: italic;">2 rows omitted</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">era</th><th style = "text-align: left;">variable</th><th style = "text-align: left;">value</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String" style = "text-align: left;">String</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">10</td><td style = "text-align: left;">00</td><td style = "text-align: right;">1591</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20</td><td style = "text-align: left;">00</td><td style = "text-align: right;">72940</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">30</td><td style = "text-align: left;">00</td><td style = "text-align: right;">177322</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">40</td><td style = "text-align: left;">00</td><td style = "text-align: right;">19355</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">50</td><td style = "text-align: left;">00</td><td style = "text-align: right;">54320</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">60</td><td style = "text-align: left;">00</td><td style = "text-align: right;">272469</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">70</td><td style = "text-align: left;">00</td><td style = "text-align: right;">13435</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">80</td><td style = "text-align: left;">00</td><td style = "text-align: right;">46360</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">90</td><td style = "text-align: left;">00</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">10</td><td style = "text-align: left;">01</td><td style = "text-align: right;">149836</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">11</td><td style = "text-align: right;">20</td><td style = "text-align: left;">01</td><td style = "text-align: right;">1363724</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">12</td><td style = "text-align: right;">30</td><td style = "text-align: left;">01</td><td style = "text-align: right;">693047</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">13</td><td style = "text-align: right;">40</td><td style = "text-align: left;">01</td><td style = "text-align: right;">9320791</td></tr><tr><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">16</td><td style = "text-align: right;">70</td><td style = "text-align: left;">01</td><td style = "text-align: right;">29764</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">17</td><td style = "text-align: right;">80</td><td style = "text-align: left;">01</td><td style = "text-align: right;">262923</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">18</td><td style = "text-align: right;">90</td><td style = "text-align: left;">01</td><td style = "text-align: right;">6260</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">19</td><td style = "text-align: right;">10</td><td style = "text-align: left;">99</td><td style = "text-align: right;">4317</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">20</td><td style = "text-align: right;">20</td><td style = "text-align: left;">99</td><td style = "text-align: right;">44328</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">21</td><td style = "text-align: right;">30</td><td style = "text-align: left;">99</td><td style = "text-align: right;">50441</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">22</td><td style = "text-align: right;">40</td><td style = "text-align: left;">99</td><td style = "text-align: right;">483512</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">23</td><td style = "text-align: right;">50</td><td style = "text-align: left;">99</td><td style = "text-align: right;">342923</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">24</td><td style = "text-align: right;">60</td><td style = "text-align: left;">99</td><td style = "text-align: right;">71418</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">25</td><td style = "text-align: right;">70</td><td style = "text-align: left;">99</td><td style = "text-align: right;">2427</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">26</td><td style = "text-align: right;">80</td><td style = "text-align: left;">99</td><td style = "text-align: right;">5111</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">27</td><td style = "text-align: right;">90</td><td style = "text-align: left;">99</td><td style = "text-align: right;">0</td></tr></tbody></table></div>

```julia
# 別解
df2 |>
    @rename(:variable => :gender_cd, :value => :amount) |>
    @orderby(_.gender_cd) |>
    @thenby(_.era) |>
    # @filter(_.amount > 0) |>
    DataFrame

```

<div><div style = "float: left;"><span>27×3 DataFrame</span></div><div style = "float: right;"><span style = "font-style: italic;">2 rows omitted</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">era</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String" style = "text-align: left;">String</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">10</td><td style = "text-align: left;">00</td><td style = "text-align: right;">1591</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20</td><td style = "text-align: left;">00</td><td style = "text-align: right;">72940</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">30</td><td style = "text-align: left;">00</td><td style = "text-align: right;">177322</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">40</td><td style = "text-align: left;">00</td><td style = "text-align: right;">19355</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">50</td><td style = "text-align: left;">00</td><td style = "text-align: right;">54320</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">60</td><td style = "text-align: left;">00</td><td style = "text-align: right;">272469</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">70</td><td style = "text-align: left;">00</td><td style = "text-align: right;">13435</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">80</td><td style = "text-align: left;">00</td><td style = "text-align: right;">46360</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">90</td><td style = "text-align: left;">00</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">10</td><td style = "text-align: left;">01</td><td style = "text-align: right;">149836</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">11</td><td style = "text-align: right;">20</td><td style = "text-align: left;">01</td><td style = "text-align: right;">1363724</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">12</td><td style = "text-align: right;">30</td><td style = "text-align: left;">01</td><td style = "text-align: right;">693047</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">13</td><td style = "text-align: right;">40</td><td style = "text-align: left;">01</td><td style = "text-align: right;">9320791</td></tr><tr><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td><td style = "text-align: right;">&vellip;</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">16</td><td style = "text-align: right;">70</td><td style = "text-align: left;">01</td><td style = "text-align: right;">29764</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">17</td><td style = "text-align: right;">80</td><td style = "text-align: left;">01</td><td style = "text-align: right;">262923</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">18</td><td style = "text-align: right;">90</td><td style = "text-align: left;">01</td><td style = "text-align: right;">6260</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">19</td><td style = "text-align: right;">10</td><td style = "text-align: left;">99</td><td style = "text-align: right;">4317</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">20</td><td style = "text-align: right;">20</td><td style = "text-align: left;">99</td><td style = "text-align: right;">44328</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">21</td><td style = "text-align: right;">30</td><td style = "text-align: left;">99</td><td style = "text-align: right;">50441</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">22</td><td style = "text-align: right;">40</td><td style = "text-align: left;">99</td><td style = "text-align: right;">483512</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">23</td><td style = "text-align: right;">50</td><td style = "text-align: left;">99</td><td style = "text-align: right;">342923</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">24</td><td style = "text-align: right;">60</td><td style = "text-align: left;">99</td><td style = "text-align: right;">71418</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">25</td><td style = "text-align: right;">70</td><td style = "text-align: left;">99</td><td style = "text-align: right;">2427</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">26</td><td style = "text-align: right;">80</td><td style = "text-align: left;">99</td><td style = "text-align: right;">5111</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">27</td><td style = "text-align: right;">90</td><td style = "text-align: left;">99</td><td style = "text-align: right;">0</td></tr></tbody></table></div>

# 日付データ

## 22.　日付型を文字列に変換

---
> J-045: 顧客データ（df_customer）の生年月日（birth_day）は日付型でデータを保有している。これをYYYYMMDD形式の文字列に変換し、顧客ID（customer_id）とともに10件表示せよ。

```julia
first(df_customer[!, [:birth_day]], 10)
```

<div><div style = "float: left;"><span>10×1 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">birth_day</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Date" style = "text-align: left;">Date</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">1981-04-29</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">1952-04-01</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">1976-10-04</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">1933-03-27</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">1995-03-29</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">1974-09-15</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">1977-08-09</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">1973-08-17</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">1931-05-02</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">1962-07-11</td></tr></tbody></table></div>

```julia
using Dates
df = df_customer[!, [:customer_id]]
df.birthday = Dates.format.(df_customer.birth_day, "yyyymmdd")
first(df, 10)
```

<div><div style = "float: left;"><span>10×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">birthday</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS021313000114</td><td style = "text-align: left;">19810429</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS037613000071</td><td style = "text-align: left;">19520401</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS031415000172</td><td style = "text-align: left;">19761004</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS028811000001</td><td style = "text-align: left;">19330327</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001215000145</td><td style = "text-align: left;">19950329</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS020401000016</td><td style = "text-align: left;">19740915</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS015414000103</td><td style = "text-align: left;">19770809</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS029403000008</td><td style = "text-align: left;">19730817</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS015804000004</td><td style = "text-align: left;">19310502</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS033513000180</td><td style = "text-align: left;">19620711</td></tr></tbody></table></div>

```julia
# 別解
df_customer |>
    @mutate(birthday=Dates.format(_.birth_day, "yyyymmdd")) |>
    @select(:customer_id, :birthday) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">birthday</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS021313000114</td><td style = "text-align: left;">19810429</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS037613000071</td><td style = "text-align: left;">19520401</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS031415000172</td><td style = "text-align: left;">19761004</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS028811000001</td><td style = "text-align: left;">19330327</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001215000145</td><td style = "text-align: left;">19950329</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS020401000016</td><td style = "text-align: left;">19740915</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS015414000103</td><td style = "text-align: left;">19770809</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS029403000008</td><td style = "text-align: left;">19730817</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS015804000004</td><td style = "text-align: left;">19310502</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS033513000180</td><td style = "text-align: left;">19620711</td></tr></tbody></table></div>

## 23.　文字列を日付型に変換

---
> J-046: 顧客データ（df_customer）の申し込み日（application_date）はYYYYMMDD形式の文字列型でデータを保有している。これを日付型に変換し、顧客ID（customer_id）とともに10件表示せよ。

```julia
df =df_customer[!, [:customer_id]]
str = df_customer.application_date
yyyy = [parse(Int, s[1:4]) for s in str];
mm = [parse(Int, s[5:6]) for s in str];
dd = [parse(Int, s[7:8]) for s in str];
df.application_date = Date.(yyyy, mm, dd)
first(df, 10)
```

<div><div style = "float: left;"><span>10×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">application_date</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Date" style = "text-align: left;">Date</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS021313000114</td><td style = "text-align: left;">2015-09-05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS037613000071</td><td style = "text-align: left;">2015-04-14</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS031415000172</td><td style = "text-align: left;">2015-05-29</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS028811000001</td><td style = "text-align: left;">2016-01-15</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001215000145</td><td style = "text-align: left;">2017-06-05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS020401000016</td><td style = "text-align: left;">2015-02-25</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS015414000103</td><td style = "text-align: left;">2015-07-22</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS029403000008</td><td style = "text-align: left;">2015-05-15</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS015804000004</td><td style = "text-align: left;">2015-06-07</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS033513000180</td><td style = "text-align: left;">2015-07-28</td></tr></tbody></table></div>

```julia
# 別解
df_customer |>
    @select(:customer_id, :application_date) |>
    @mutate(yyyy = parse(Int, _.application_date[1:4]),
        mm = parse(Int, _.application_date[5:6]),
        dd = parse(Int, _.application_date[7:8])) |>
    @mutate(application_date = Date(_.yyyy, _.mm, _.dd)) |>
    @select(:customer_id, :application_date) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">application_date</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Date" style = "text-align: left;">Date</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS021313000114</td><td style = "text-align: left;">2015-09-05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS037613000071</td><td style = "text-align: left;">2015-04-14</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS031415000172</td><td style = "text-align: left;">2015-05-29</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS028811000001</td><td style = "text-align: left;">2016-01-15</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001215000145</td><td style = "text-align: left;">2017-06-05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS020401000016</td><td style = "text-align: left;">2015-02-25</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS015414000103</td><td style = "text-align: left;">2015-07-22</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS029403000008</td><td style = "text-align: left;">2015-05-15</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS015804000004</td><td style = "text-align: left;">2015-06-07</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS033513000180</td><td style = "text-align: left;">2015-07-28</td></tr></tbody></table></div>

---
> J-047: レシート明細データ（df_receipt）の売上日（sales_ymd）はYYYYMMDD形式の数値型でデータを保有している。これを日付型に変換し、レシート番号（receipt_no）、レシートサブ番号（receipt_sub_no）とともに10件表示せよ。

```julia
df = df_receipt[!, [:receipt_no, :receipt_sub_no]]
yyyy = @. df_receipt.sales_ymd ÷ 10000;
mm = @. df_receipt.sales_ymd % 10000 ÷ 100;
dd = @. df_receipt.sales_ymd % 100;
df.sales_ymd = Date.(yyyy, mm, dd)
first(df, 10)
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">receipt_no</th><th style = "text-align: left;">receipt_sub_no</th><th style = "text-align: left;">sales_ymd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Date" style = "text-align: left;">Date</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2018-11-03</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2018-11-18</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2017-07-12</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2019-02-05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2018-08-21</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2019-06-05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2018-12-05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2019-09-22</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2017-05-04</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2019-10-10</td></tr></tbody></table></div>

```julia
# 別解
df_receipt |>
    @select(:receipt_no, :receipt_sub_no, :sales_ymd) |>
    @mutate(yyyy = _.sales_ymd ÷ 10000,
        mm = _.sales_ymd % 10000 ÷ 100,
        dd = _.sales_ymd % 100) |>
    @mutate(salse_ymd = Date.(_.yyyy, _.mm, _.dd)) |>
    @select(:receipt_no, :receipt_sub_no, :salse_ymd) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">receipt_no</th><th style = "text-align: left;">receipt_sub_no</th><th style = "text-align: left;">salse_ymd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Date" style = "text-align: left;">Date</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2018-11-03</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2018-11-18</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2017-07-12</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2019-02-05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2018-08-21</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2019-06-05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2018-12-05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2019-09-22</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2017-05-04</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2019-10-10</td></tr></tbody></table></div>

---
> J-048: レシート明細データ（df_receipt）の売上エポック秒（sales_epoch）は数値型のUNIX秒でデータを保有している。これを日付型に変換し、レシート番号(receipt_no)、レシートサブ番号（receipt_sub_no）とともに10件表示せよ。

```julia
df = df_receipt[!, [:receipt_no, :receipt_sub_no]]
df.sales_ymd = Date.(unix2datetime.(df_receipt.sales_epoch))
first(df, 10)
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">receipt_no</th><th style = "text-align: left;">receipt_sub_no</th><th style = "text-align: left;">sales_ymd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Date" style = "text-align: left;">Date</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2018-11-03</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2018-11-18</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2017-07-12</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2019-02-05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2018-08-21</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2019-06-05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2018-12-05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2019-09-22</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2017-05-04</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2019-10-10</td></tr></tbody></table></div>

```julia
# 別解
df_receipt |>
    @mutate(sales_ymd = Date(unix2datetime(_.sales_epoch))) |>
    @select(:receipt_no, :receipt_sub_no, :sales_ymd) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">receipt_no</th><th style = "text-align: left;">receipt_sub_no</th><th style = "text-align: left;">sales_ymd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Date" style = "text-align: left;">Date</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2018-11-03</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2018-11-18</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2017-07-12</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2019-02-05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2018-08-21</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2019-06-05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2018-12-05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2019-09-22</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2017-05-04</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2019-10-10</td></tr></tbody></table></div>

## 24.　エポック秒を日付型に変換

---
> J-049: レシート明細データ（df_receipt）の売上エポック秒（sales_epoch）を日付型に変換し、「年」だけ取り出してレシート番号(receipt_no)、レシートサブ番号（receipt_sub_no）とともに10件表示せよ。

```julia
df = df_receipt[!, [:receipt_no, :receipt_sub_no]]
df.salse_year = year.(unix2datetime.(df_receipt.sales_epoch))
first(df, 10)
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">receipt_no</th><th style = "text-align: left;">receipt_sub_no</th><th style = "text-align: left;">salse_year</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">112</td><td style = "text-align: right;">1</td><td style = "text-align: right;">2018</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">2</td><td style = "text-align: right;">2018</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: right;">2017</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">1</td><td style = "text-align: right;">2019</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: right;">2018</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">1</td><td style = "text-align: right;">2019</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: right;">2018</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: right;">2019</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">2</td><td style = "text-align: right;">2017</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: right;">2019</td></tr></tbody></table></div>

```julia
#別解
df_receipt |>
    @mutate(salse_year = year(unix2datetime(_.sales_epoch))) |>
    @select(:receipt_no, :receipt_sub_no, :salse_year) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">receipt_no</th><th style = "text-align: left;">receipt_sub_no</th><th style = "text-align: left;">salse_year</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">112</td><td style = "text-align: right;">1</td><td style = "text-align: right;">2018</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">2</td><td style = "text-align: right;">2018</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: right;">2017</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">1</td><td style = "text-align: right;">2019</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: right;">2018</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">1</td><td style = "text-align: right;">2019</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: right;">2018</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: right;">2019</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">2</td><td style = "text-align: right;">2017</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: right;">2019</td></tr></tbody></table></div>

---
> J-050: レシート明細データ（df_receipt）の売上エポック秒（sales_epoch）を日付型に変換し、「月」だけ取り出してレシート番号(receipt_no)、レシートサブ番号（receipt_sub_no）とともに10件表示せよ。なお、「月」は0埋め2桁で取り出すこと。

```julia
df = df_receipt[!, [:receipt_no, :receipt_sub_no]]
df.salse_month = @. string(month(unix2datetime(df_receipt.sales_epoch)), pad=2)
first(df, 10)
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">receipt_no</th><th style = "text-align: left;">receipt_sub_no</th><th style = "text-align: left;">salse_month</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String" style = "text-align: left;">String</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">11</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">2</td><td style = "text-align: left;">11</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">07</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">1</td><td style = "text-align: left;">02</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">08</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">06</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">12</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">09</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">2</td><td style = "text-align: left;">05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">10</td></tr></tbody></table></div>

```julia
# 別解
df_receipt |>
    @mutate(salse_month = string(month(unix2datetime(_.sales_epoch)), pad=2)) |>
    @select(:receipt_no, :receipt_sub_no, :salse_month) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">receipt_no</th><th style = "text-align: left;">receipt_sub_no</th><th style = "text-align: left;">salse_month</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String" style = "text-align: left;">String</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">11</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">2</td><td style = "text-align: left;">11</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">07</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">1</td><td style = "text-align: left;">02</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">08</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">06</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">12</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">09</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">2</td><td style = "text-align: left;">05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">10</td></tr></tbody></table></div>

---
> J-051: レシート明細データ（df_receipt）の売上エポック秒を日付型に変換し、「日」だけ取り出してレシート番号(receipt_no)、レシートサブ番号（receipt_sub_no）とともに10件表示せよ。なお、「日」は0埋め2桁で取り出すこと。

```julia
df = df_receipt[!, [:receipt_no, :receipt_sub_no]]
df.salse_day = @. string(day(unix2datetime(df_receipt.sales_epoch)), pad=2)
first(df, 10)
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">receipt_no</th><th style = "text-align: left;">receipt_sub_no</th><th style = "text-align: left;">salse_day</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String" style = "text-align: left;">String</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">03</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">2</td><td style = "text-align: left;">18</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">12</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">1</td><td style = "text-align: left;">05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">21</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">22</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">2</td><td style = "text-align: left;">04</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">10</td></tr></tbody></table></div>

```julia
# 別解
df_receipt |>
    @mutate(salse_day = string(day(unix2datetime(_.sales_epoch)), pad=2)) |>
    @select(:receipt_no, :receipt_sub_no, :salse_day) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">receipt_no</th><th style = "text-align: left;">receipt_sub_no</th><th style = "text-align: left;">salse_day</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String" style = "text-align: left;">String</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">03</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">2</td><td style = "text-align: left;">18</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">12</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">1132</td><td style = "text-align: right;">1</td><td style = "text-align: left;">05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">21</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">1</td><td style = "text-align: left;">05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">2</td><td style = "text-align: left;">05</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">22</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">1112</td><td style = "text-align: right;">2</td><td style = "text-align: left;">04</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">1102</td><td style = "text-align: right;">1</td><td style = "text-align: left;">10</td></tr></tbody></table></div>

# 変数の作成と数式変形

---
> J-052: レシート明細データ（df_receipt）の売上金額（amount）を顧客ID（customer_id）ごとに合計の上、売上金額合計に対して2,000円以下を0、2,000円より大きい金額を1に二値化し、顧客ID、売上金額合計とともに10件表示せよ。ただし、顧客IDが"Z"から始まるのものは非会員を表すため、除外して計算すること。

```julia
gd = groupby(filter(:customer_id => x -> !occursin(r"^Z", x), df_receipt), :customer_id)
df2 = combine(gd, :amount => sum => :sum_amount)
df2.salse_flg = (df2.sum_amount .> 2000) .+ 0
df3 = sort(df2, :customer_id)
first(df3, 10)
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sum_amount</th><th style = "text-align: left;">salse_flg</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: right;">1298</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">626</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">3044</td><td style = "text-align: right;">1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: right;">1988</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">3337</td><td style = "text-align: right;">1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001211000025</td><td style = "text-align: right;">456</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001212000027</td><td style = "text-align: right;">448</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001212000031</td><td style = "text-align: right;">296</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001212000046</td><td style = "text-align: right;">228</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001212000070</td><td style = "text-align: right;">456</td><td style = "text-align: right;">0</td></tr></tbody></table></div>

```julia
# 別解
df_receipt |>
    @filter(!occursin(r"^Z", _.customer_id)) |>
    @groupby(_.customer_id) |>
    @map({customer_id = key(_), sum_amount = sum(_.amount)}) |>
    @mutate(salse_flg = (_.sum_amount > 2000) + 0) |>
    @orderby(_.customer_id) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sum_amount</th><th style = "text-align: left;">salse_flg</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: right;">1298</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">626</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">3044</td><td style = "text-align: right;">1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: right;">1988</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">3337</td><td style = "text-align: right;">1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001211000025</td><td style = "text-align: right;">456</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001212000027</td><td style = "text-align: right;">448</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001212000031</td><td style = "text-align: right;">296</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001212000046</td><td style = "text-align: right;">228</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001212000070</td><td style = "text-align: right;">456</td><td style = "text-align: right;">0</td></tr></tbody></table></div>

---
> J-053: 顧客データ（df_customer）の郵便番号（postal_cd）に対し、東京（先頭3桁が100〜209のもの）を1、それ以外のものを0に二値化せよ。さらにレシート明細データ（df_receipt）と結合し、全期間において売上実績のある顧客数を、作成した二値ごとにカウントせよ。

```julia
df2 = df_customer[!, [:customer_id, :postal_cd]]
df2.postal_flg = @. (100 <= parse(Int, SubString(df2.postal_cd, 1, 3)) <= 209) + 0
df3 = unique(innerjoin(df2, df_receipt[!, [:customer_id]], on=:customer_id))
combine(groupby(df3, :postal_flg), nrow)
```

<div><div style = "float: left;"><span>2×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">postal_flg</th><th style = "text-align: left;">nrow</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">0</td><td style = "text-align: right;">3906</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">1</td><td style = "text-align: right;">4400</td></tr></tbody></table></div>

```julia
# 別解
using StatsBase
df2 = df_customer[!, [:customer_id, :postal_cd]] |>
    @mutate(postal_flg = (100 <= parse(Int, SubString(_.postal_cd, 1, 3)) <= 209) + 0) |>
    DataFrame;
df3 = innerjoin(df2, df_receipt[!, [:customer_id]], on=:customer_id) |>
    @unique() |>
    @groupby(_.postal_flg) |>
    @map({postal_flg = key(_), nrow = length(_.postal_flg)}) |>
    DataFrame
```

<div><div style = "float: left;"><span>2×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">postal_flg</th><th style = "text-align: left;">nrow</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">0</td><td style = "text-align: right;">3906</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">1</td><td style = "text-align: right;">4400</td></tr></tbody></table></div>

---
> J-054: 顧客データ（df_customer）の住所（address）は、埼玉県、千葉県、東京都、神奈川県のいずれかとなっている。都道府県毎にコード値を作成し、顧客ID、住所とともに10件表示せよ。値は埼玉県を11、千葉県を12、東京都を13、神奈川県を14とすること。

```julia
df = df_customer[!, [:customer_id, :address]]
df.prefecture_cd = [indexin(s[1], ['埼', '千', '東', '神'])[1]+10 for s in df.address];
first(df, 10)
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">address</th><th style = "text-align: left;">prefecture_cd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS021313000114</td><td style = "text-align: left;">神奈川県伊勢原市粟窪**********</td><td style = "text-align: right;">14</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS037613000071</td><td style = "text-align: left;">東京都江東区南砂**********</td><td style = "text-align: right;">13</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS031415000172</td><td style = "text-align: left;">東京都渋谷区代々木**********</td><td style = "text-align: right;">13</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS028811000001</td><td style = "text-align: left;">神奈川県横浜市泉区和泉町**********</td><td style = "text-align: right;">14</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001215000145</td><td style = "text-align: left;">東京都大田区仲六郷**********</td><td style = "text-align: right;">13</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS020401000016</td><td style = "text-align: left;">東京都板橋区若木**********</td><td style = "text-align: right;">13</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS015414000103</td><td style = "text-align: left;">東京都江東区北砂**********</td><td style = "text-align: right;">13</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS029403000008</td><td style = "text-align: left;">千葉県浦安市海楽**********</td><td style = "text-align: right;">12</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS015804000004</td><td style = "text-align: left;">東京都江東区北砂**********</td><td style = "text-align: right;">13</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS033513000180</td><td style = "text-align: left;">神奈川県横浜市旭区善部町**********</td><td style = "text-align: right;">14</td></tr></tbody></table></div>

```julia
# 別解
df_customer[!, [:customer_id, :address]] |>
    @mutate(prefecture_cd = occursin(r"^東京都", _.address) ? 13 :
            (occursin(r"^千葉県", _.address) ? 12 :
            (occursin(r"^埼玉県", _.address) ? 11 : 14))) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">address</th><th style = "text-align: left;">prefecture_cd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS021313000114</td><td style = "text-align: left;">神奈川県伊勢原市粟窪**********</td><td style = "text-align: right;">14</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS037613000071</td><td style = "text-align: left;">東京都江東区南砂**********</td><td style = "text-align: right;">13</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS031415000172</td><td style = "text-align: left;">東京都渋谷区代々木**********</td><td style = "text-align: right;">13</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS028811000001</td><td style = "text-align: left;">神奈川県横浜市泉区和泉町**********</td><td style = "text-align: right;">14</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001215000145</td><td style = "text-align: left;">東京都大田区仲六郷**********</td><td style = "text-align: right;">13</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS020401000016</td><td style = "text-align: left;">東京都板橋区若木**********</td><td style = "text-align: right;">13</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS015414000103</td><td style = "text-align: left;">東京都江東区北砂**********</td><td style = "text-align: right;">13</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS029403000008</td><td style = "text-align: left;">千葉県浦安市海楽**********</td><td style = "text-align: right;">12</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS015804000004</td><td style = "text-align: left;">東京都江東区北砂**********</td><td style = "text-align: right;">13</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS033513000180</td><td style = "text-align: left;">神奈川県横浜市旭区善部町**********</td><td style = "text-align: right;">14</td></tr></tbody></table></div>

---
> J-055: レシート明細（df_receipt）データの売上金額（amount）を顧客ID（customer_id）ごとに合計し、その合計金額の四分位点を求めよ。その上で、顧客ごとの売上金額合計に対して以下の基準でカテゴリ値を作成し、顧客ID、売上金額合計とともに10件表示せよ。カテゴリ値は順に1〜4とする。
>
> - 最小値以上第1四分位未満 ・・・ 1を付与
> - 第1四分位以上第2四分位未満 ・・・ 2を付与
> - 第2四分位以上第3四分位未満 ・・・ 3を付与
> - 第3四分位以上 ・・・ 4を付与

```julia
function encode(df)
    for x in df
        x < Q[1] && return 1
        x < Q[2] && return 2
        x < Q[3] && return 3
        return 4
    end
end

using StatsBase
gd = groupby(df_receipt[:, [:amount, :customer_id]], :customer_id)
df = combine(gd, :amount => sum);
Q = quantile(df.amount_sum, [0.25, 0.50, 0.75]);

df.pct_group = encode.(df.amount_sum);
df2 = sort(df, :customer_id)
first(df2, 10)
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">amount_sum</th><th style = "text-align: left;">pct_group</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: right;">1298</td><td style = "text-align: right;">2</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">626</td><td style = "text-align: right;">2</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">3044</td><td style = "text-align: right;">3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: right;">1988</td><td style = "text-align: right;">3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">3337</td><td style = "text-align: right;">3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001211000025</td><td style = "text-align: right;">456</td><td style = "text-align: right;">1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001212000027</td><td style = "text-align: right;">448</td><td style = "text-align: right;">1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001212000031</td><td style = "text-align: right;">296</td><td style = "text-align: right;">1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001212000046</td><td style = "text-align: right;">228</td><td style = "text-align: right;">1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001212000070</td><td style = "text-align: right;">456</td><td style = "text-align: right;">1</td></tr></tbody></table></div>

```julia
Q
```

    3-element Vector{Float64}:
      548.5
     1478.0
     3651.0

```julia
# 別解
using StatsBase
gd = groupby(df_receipt[:, [:amount, :customer_id]], :customer_id)
df = combine(gd, :amount => sum);
Q = quantile(df.amount_sum, [0.25, 0.50, 0.75]);

df2 = df |>
    @mutate(pct_group = _.amount_sum .< Q[1] ? 1 :
        (_.amount_sum .< Q[2] ? 2 :
        (_.amount_sum .< Q[3] ? 3 : 4))) |>
    @orderby(_.customer_id) |>
    DataFrame
first(df2, 10)
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">amount_sum</th><th style = "text-align: left;">pct_group</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: right;">1298</td><td style = "text-align: right;">2</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">626</td><td style = "text-align: right;">2</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">3044</td><td style = "text-align: right;">3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: right;">1988</td><td style = "text-align: right;">3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">3337</td><td style = "text-align: right;">3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001211000025</td><td style = "text-align: right;">456</td><td style = "text-align: right;">1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001212000027</td><td style = "text-align: right;">448</td><td style = "text-align: right;">1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001212000031</td><td style = "text-align: right;">296</td><td style = "text-align: right;">1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001212000046</td><td style = "text-align: right;">228</td><td style = "text-align: right;">1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001212000070</td><td style = "text-align: right;">456</td><td style = "text-align: right;">1</td></tr></tbody></table></div>

---
> J-056: 顧客データ（df_customer）の年齢（age）をもとに10歳刻みで年代を算出し、顧客ID（customer_id）、生年月日（birth_day）とともに10件表示せよ。ただし、60歳以上は全て60歳代とすること。年代を表すカテゴリ名は任意とする。

```julia
df = df_customer[!, [:customer_id, :birth_day, :age]]
df.era = @. min((df.age ÷ 10)*10, 60)
first(df, 10)
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">era</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS021313000114</td><td style = "text-align: left;">1981-04-29</td><td style = "text-align: right;">37</td><td style = "text-align: right;">30</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS037613000071</td><td style = "text-align: left;">1952-04-01</td><td style = "text-align: right;">66</td><td style = "text-align: right;">60</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS031415000172</td><td style = "text-align: left;">1976-10-04</td><td style = "text-align: right;">42</td><td style = "text-align: right;">40</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS028811000001</td><td style = "text-align: left;">1933-03-27</td><td style = "text-align: right;">86</td><td style = "text-align: right;">60</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001215000145</td><td style = "text-align: left;">1995-03-29</td><td style = "text-align: right;">24</td><td style = "text-align: right;">20</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS020401000016</td><td style = "text-align: left;">1974-09-15</td><td style = "text-align: right;">44</td><td style = "text-align: right;">40</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS015414000103</td><td style = "text-align: left;">1977-08-09</td><td style = "text-align: right;">41</td><td style = "text-align: right;">40</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS029403000008</td><td style = "text-align: left;">1973-08-17</td><td style = "text-align: right;">45</td><td style = "text-align: right;">40</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS015804000004</td><td style = "text-align: left;">1931-05-02</td><td style = "text-align: right;">87</td><td style = "text-align: right;">60</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS033513000180</td><td style = "text-align: left;">1962-07-11</td><td style = "text-align: right;">56</td><td style = "text-align: right;">50</td></tr></tbody></table></div>

```julia
# 別解
df_customer[!, [:customer_id, :birth_day, :age]] |>
    @mutate(era = min((_.age ÷ 10)*10, 60)) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">era</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS021313000114</td><td style = "text-align: left;">1981-04-29</td><td style = "text-align: right;">37</td><td style = "text-align: right;">30</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS037613000071</td><td style = "text-align: left;">1952-04-01</td><td style = "text-align: right;">66</td><td style = "text-align: right;">60</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS031415000172</td><td style = "text-align: left;">1976-10-04</td><td style = "text-align: right;">42</td><td style = "text-align: right;">40</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS028811000001</td><td style = "text-align: left;">1933-03-27</td><td style = "text-align: right;">86</td><td style = "text-align: right;">60</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001215000145</td><td style = "text-align: left;">1995-03-29</td><td style = "text-align: right;">24</td><td style = "text-align: right;">20</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS020401000016</td><td style = "text-align: left;">1974-09-15</td><td style = "text-align: right;">44</td><td style = "text-align: right;">40</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS015414000103</td><td style = "text-align: left;">1977-08-09</td><td style = "text-align: right;">41</td><td style = "text-align: right;">40</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS029403000008</td><td style = "text-align: left;">1973-08-17</td><td style = "text-align: right;">45</td><td style = "text-align: right;">40</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS015804000004</td><td style = "text-align: left;">1931-05-02</td><td style = "text-align: right;">87</td><td style = "text-align: right;">60</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS033513000180</td><td style = "text-align: left;">1962-07-11</td><td style = "text-align: right;">56</td><td style = "text-align: right;">50</td></tr></tbody></table></div>

---
> J-057: 056の抽出結果と性別コード（gender_cd）により、新たに性別×年代の組み合わせを表すカテゴリデータを作成し、10件表示せよ。組み合わせを表すカテゴリの値は任意とする。

```julia
df = df_customer[!, [:customer_id, :gender_cd, :birth_day, :age]]
df.era = @. min((df.age ÷ 10)*10, 60)
df.gender_era = @. string(df.gender_cd) * string(df.era)
first(df, 10)
```

<div><div style = "float: left;"><span>10×6 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">era</th><th style = "text-align: left;">gender_era</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String" style = "text-align: left;">String</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS021313000114</td><td style = "text-align: left;">1</td><td style = "text-align: left;">1981-04-29</td><td style = "text-align: right;">37</td><td style = "text-align: right;">30</td><td style = "text-align: left;">130</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS037613000071</td><td style = "text-align: left;">9</td><td style = "text-align: left;">1952-04-01</td><td style = "text-align: right;">66</td><td style = "text-align: right;">60</td><td style = "text-align: left;">960</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS031415000172</td><td style = "text-align: left;">1</td><td style = "text-align: left;">1976-10-04</td><td style = "text-align: right;">42</td><td style = "text-align: right;">40</td><td style = "text-align: left;">140</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS028811000001</td><td style = "text-align: left;">1</td><td style = "text-align: left;">1933-03-27</td><td style = "text-align: right;">86</td><td style = "text-align: right;">60</td><td style = "text-align: left;">160</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001215000145</td><td style = "text-align: left;">1</td><td style = "text-align: left;">1995-03-29</td><td style = "text-align: right;">24</td><td style = "text-align: right;">20</td><td style = "text-align: left;">120</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS020401000016</td><td style = "text-align: left;">0</td><td style = "text-align: left;">1974-09-15</td><td style = "text-align: right;">44</td><td style = "text-align: right;">40</td><td style = "text-align: left;">040</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS015414000103</td><td style = "text-align: left;">1</td><td style = "text-align: left;">1977-08-09</td><td style = "text-align: right;">41</td><td style = "text-align: right;">40</td><td style = "text-align: left;">140</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS029403000008</td><td style = "text-align: left;">0</td><td style = "text-align: left;">1973-08-17</td><td style = "text-align: right;">45</td><td style = "text-align: right;">40</td><td style = "text-align: left;">040</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS015804000004</td><td style = "text-align: left;">0</td><td style = "text-align: left;">1931-05-02</td><td style = "text-align: right;">87</td><td style = "text-align: right;">60</td><td style = "text-align: left;">060</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS033513000180</td><td style = "text-align: left;">1</td><td style = "text-align: left;">1962-07-11</td><td style = "text-align: right;">56</td><td style = "text-align: right;">50</td><td style = "text-align: left;">150</td></tr></tbody></table></div>

```julia
# 別解
df_customer[!, [:customer_id, :gender_cd, :birth_day, :age]] |>
    @mutate(era = min((_.age ÷ 10)*10, 60)) |>
    @mutate(gender_era = string(_.gender_cd) * string(_.era)) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×6 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">era</th><th style = "text-align: left;">gender_era</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String" style = "text-align: left;">String</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS021313000114</td><td style = "text-align: left;">1</td><td style = "text-align: left;">1981-04-29</td><td style = "text-align: right;">37</td><td style = "text-align: right;">30</td><td style = "text-align: left;">130</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS037613000071</td><td style = "text-align: left;">9</td><td style = "text-align: left;">1952-04-01</td><td style = "text-align: right;">66</td><td style = "text-align: right;">60</td><td style = "text-align: left;">960</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS031415000172</td><td style = "text-align: left;">1</td><td style = "text-align: left;">1976-10-04</td><td style = "text-align: right;">42</td><td style = "text-align: right;">40</td><td style = "text-align: left;">140</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS028811000001</td><td style = "text-align: left;">1</td><td style = "text-align: left;">1933-03-27</td><td style = "text-align: right;">86</td><td style = "text-align: right;">60</td><td style = "text-align: left;">160</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001215000145</td><td style = "text-align: left;">1</td><td style = "text-align: left;">1995-03-29</td><td style = "text-align: right;">24</td><td style = "text-align: right;">20</td><td style = "text-align: left;">120</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS020401000016</td><td style = "text-align: left;">0</td><td style = "text-align: left;">1974-09-15</td><td style = "text-align: right;">44</td><td style = "text-align: right;">40</td><td style = "text-align: left;">040</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS015414000103</td><td style = "text-align: left;">1</td><td style = "text-align: left;">1977-08-09</td><td style = "text-align: right;">41</td><td style = "text-align: right;">40</td><td style = "text-align: left;">140</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS029403000008</td><td style = "text-align: left;">0</td><td style = "text-align: left;">1973-08-17</td><td style = "text-align: right;">45</td><td style = "text-align: right;">40</td><td style = "text-align: left;">040</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS015804000004</td><td style = "text-align: left;">0</td><td style = "text-align: left;">1931-05-02</td><td style = "text-align: right;">87</td><td style = "text-align: right;">60</td><td style = "text-align: left;">060</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS033513000180</td><td style = "text-align: left;">1</td><td style = "text-align: left;">1962-07-11</td><td style = "text-align: right;">56</td><td style = "text-align: right;">50</td><td style = "text-align: left;">150</td></tr></tbody></table></div>

## 25.　ダミー変数の作成

---
> J-058: 顧客データ（df_customer）の性別コード（gender_cd）をダミー変数化し、顧客ID（customer_id）とともに10件表示せよ。

```julia
# トリッキー（スマート）な方法
dummydatamatrix = Matrix(permutedims(["0", "1", "9"]) .== df_customer.gender_cd) .+ 0
df = hcat(df_customer[!, [:customer_id]], DataFrame(dummydatamatrix, :auto))
rename!(df, [:customer_id, :gender_cd0, :gender_cd1, :gender_cd9])
first(df, 10)
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">gender_cd0</th><th style = "text-align: left;">gender_cd1</th><th style = "text-align: left;">gender_cd9</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS021313000114</td><td style = "text-align: right;">0</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS037613000071</td><td style = "text-align: right;">0</td><td style = "text-align: right;">0</td><td style = "text-align: right;">1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS031415000172</td><td style = "text-align: right;">0</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS028811000001</td><td style = "text-align: right;">0</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001215000145</td><td style = "text-align: right;">0</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS020401000016</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS015414000103</td><td style = "text-align: right;">0</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS029403000008</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS015804000004</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS033513000180</td><td style = "text-align: right;">0</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td></tr></tbody></table></div>

```julia
# 地道な方法
df = df_customer[!, [:customer_id]]
df.gender_cd0 = @. (df_customer.gender_cd == "0") + 0
df.gender_cd1 = @. (df_customer.gender_cd == "1") + 0
df.gender_cd9 = @. (df_customer.gender_cd == "9") + 0
first(df, 10)
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">gender_cd0</th><th style = "text-align: left;">gender_cd1</th><th style = "text-align: left;">gender_cd9</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS021313000114</td><td style = "text-align: right;">0</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS037613000071</td><td style = "text-align: right;">0</td><td style = "text-align: right;">0</td><td style = "text-align: right;">1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS031415000172</td><td style = "text-align: right;">0</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS028811000001</td><td style = "text-align: right;">0</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001215000145</td><td style = "text-align: right;">0</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS020401000016</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS015414000103</td><td style = "text-align: right;">0</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS029403000008</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS015804000004</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS033513000180</td><td style = "text-align: right;">0</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td></tr></tbody></table></div>

```julia
# 別解
df_customer[!, [:customer_id, :gender_cd]] |>
    @mutate(gender_cd0 = (_.gender_cd == "0") + 0,
            gender_cd1 = (_.gender_cd == "1") + 0,
            gender_cd9 = (_.gender_cd == "9") + 0)|>
    @select(-:gender_cd) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">gender_cd0</th><th style = "text-align: left;">gender_cd1</th><th style = "text-align: left;">gender_cd9</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS021313000114</td><td style = "text-align: right;">0</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS037613000071</td><td style = "text-align: right;">0</td><td style = "text-align: right;">0</td><td style = "text-align: right;">1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS031415000172</td><td style = "text-align: right;">0</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS028811000001</td><td style = "text-align: right;">0</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001215000145</td><td style = "text-align: right;">0</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS020401000016</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS015414000103</td><td style = "text-align: right;">0</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS029403000008</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS015804000004</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS033513000180</td><td style = "text-align: right;">0</td><td style = "text-align: right;">1</td><td style = "text-align: right;">0</td></tr></tbody></table></div>

## 26.　標準化

---
> J-059: レシート明細データ（df_receipt）の売上金額（amount）を顧客ID（customer_id）ごとに合計し、売上金額合計を平均0、標準偏差1に標準化して顧客ID、売上金額合計とともに10件表示せよ。標準化に使用する標準偏差は、分散の平方根、もしくは不偏分散の平方根のどちらでも良いものとする。ただし、顧客IDが"Z"から始まるのものは非会員を表すため、除外して計算すること。

~~TIPS:
~- query()の引数engineで'python'か'numexpr'かを選択でき、デフォルトはインストールされていればnumexprが、無ければpythonが使われます。さらに、文字列メソッドはengine='python'でないとquery()内で使えません。~~

```julia
gd = groupby(filter(:customer_id => x -> !occursin(r"^Z", x), df_receipt), :customer_id)
df = combine(gd, :amount => sum => :sum_amount)
df.standardized_amount = (df.sum_amount .- mean(df.sum_amount)) ./ std(df.sum_amount)
df2 = sort(df, :customer_id)
first(df2, 10)
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sum_amount</th><th style = "text-align: left;">standardized_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: right;">1298</td><td style = "text-align: right;">-0.45935</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">626</td><td style = "text-align: right;">-0.706348</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">3044</td><td style = "text-align: right;">0.182403</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: right;">1988</td><td style = "text-align: right;">-0.205737</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">3337</td><td style = "text-align: right;">0.290096</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001211000025</td><td style = "text-align: right;">456</td><td style = "text-align: right;">-0.768832</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001212000027</td><td style = "text-align: right;">448</td><td style = "text-align: right;">-0.771773</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001212000031</td><td style = "text-align: right;">296</td><td style = "text-align: right;">-0.827641</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001212000046</td><td style = "text-align: right;">228</td><td style = "text-align: right;">-0.852635</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001212000070</td><td style = "text-align: right;">456</td><td style = "text-align: right;">-0.768832</td></tr></tbody></table></div>

```julia
# 別解
df2 = df_receipt[!, [:customer_id, :amount]] |>
    @filter(!occursin(r"^Z", _.customer_id)) |>
    DataFrame;
```

```julia
gd = groupby(df2, :customer_id)
df3 = combine(gd, :amount => sum => :sum_amount) |>
    @orderby(_.customer_id) |>
    DataFrame
using StatsBase
mean_amount = mean(df3.sum_amount)
std_amount  = std(df3.sum_amount);
```

```julia
df3 |>
    @mutate(standardized_amount = (_.sum_amount - mean_amount) / std_amount) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sum_amount</th><th style = "text-align: left;">standardized_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Any" style = "text-align: left;">Any</th><th title = "Any" style = "text-align: left;">Any</th><th title = "Any" style = "text-align: left;">Any</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: left;">1298</td><td style = "text-align: left;">-0.45935</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: left;">626</td><td style = "text-align: left;">-0.706348</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: left;">3044</td><td style = "text-align: left;">0.182403</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: left;">1988</td><td style = "text-align: left;">-0.205737</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: left;">3337</td><td style = "text-align: left;">0.290096</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001211000025</td><td style = "text-align: left;">456</td><td style = "text-align: left;">-0.768832</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001212000027</td><td style = "text-align: left;">448</td><td style = "text-align: left;">-0.771773</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001212000031</td><td style = "text-align: left;">296</td><td style = "text-align: left;">-0.827641</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001212000046</td><td style = "text-align: left;">228</td><td style = "text-align: left;">-0.852635</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001212000070</td><td style = "text-align: left;">456</td><td style = "text-align: left;">-0.768832</td></tr></tbody></table></div>

**df2 を作ってからは，以下のように freqtables() を使うほうが簡単でわかりやすい。**

```julia
using FreqTables
a = freqtable(df2.customer_id, weights=df2.amount)
(mean_amount, std_amount) = (mean(a), std(a))
DataFrame(customer_id=names(a)[1], sum_amount=a) |>
    @mutate(standardized_amount = (_.sum_amount - mean_amount) / std_amount) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sum_amount</th><th style = "text-align: left;">standardized_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Any" style = "text-align: left;">Any</th><th title = "Any" style = "text-align: left;">Any</th><th title = "Any" style = "text-align: left;">Any</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: left;">1298</td><td style = "text-align: left;">-0.45935</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: left;">626</td><td style = "text-align: left;">-0.706348</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: left;">3044</td><td style = "text-align: left;">0.182403</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: left;">1988</td><td style = "text-align: left;">-0.205737</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: left;">3337</td><td style = "text-align: left;">0.290096</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001211000025</td><td style = "text-align: left;">456</td><td style = "text-align: left;">-0.768832</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001212000027</td><td style = "text-align: left;">448</td><td style = "text-align: left;">-0.771773</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001212000031</td><td style = "text-align: left;">296</td><td style = "text-align: left;">-0.827641</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001212000046</td><td style = "text-align: left;">228</td><td style = "text-align: left;">-0.852635</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001212000070</td><td style = "text-align: left;">456</td><td style = "text-align: left;">-0.768832</td></tr></tbody></table></div>

---
> J-060: レシート明細データ（df_receipt）の売上金額（amount）を顧客ID（customer_id）ごとに合計し、売上金額合計を最小値0、最大値1に正規化して顧客ID、売上金額合計とともに10件表示せよ。ただし、顧客IDが"Z"から始まるのものは非会員を表すため、除外して計算すること。

```julia
gd = groupby(filter(:customer_id => x -> !occursin(r"^Z", x), df_receipt), :customer_id)
df = combine(gd, :amount => sum => :sum_amount)
df.standardized_amount = (df.sum_amount .- minimum(df.sum_amount)) ./ (maximum(df.sum_amount) - minimum(df.sum_amount))
df2 = sort(df, :customer_id)
first(df2, 10)
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sum_amount</th><th style = "text-align: left;">standardized_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: right;">1298</td><td style = "text-align: right;">0.0533542</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">626</td><td style = "text-align: right;">0.0241571</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">3044</td><td style = "text-align: right;">0.129214</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: right;">1988</td><td style = "text-align: right;">0.0833333</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">3337</td><td style = "text-align: right;">0.141945</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001211000025</td><td style = "text-align: right;">456</td><td style = "text-align: right;">0.0167709</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001212000027</td><td style = "text-align: right;">448</td><td style = "text-align: right;">0.0164234</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001212000031</td><td style = "text-align: right;">296</td><td style = "text-align: right;">0.00981926</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001212000046</td><td style = "text-align: right;">228</td><td style = "text-align: right;">0.00686479</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001212000070</td><td style = "text-align: right;">456</td><td style = "text-align: right;">0.0167709</td></tr></tbody></table></div>

```julia
# 別解
df2 = df_receipt[!, [:customer_id, :amount]] |>
    @filter(!occursin(r"^Z", _.customer_id)) |>
    DataFrame;

using FreqTables
a = freqtable(df2.customer_id, weights=df2.amount)
(min_amount, max_amount) = (minimum(a), maximum(a))
DataFrame(customer_id=names(a)[1], sum_amount=a) |>
    @mutate(scaled_amount = (_.sum_amount - min_amount) / (max_amount - min_amount)) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sum_amount</th><th style = "text-align: left;">scaled_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Any" style = "text-align: left;">Any</th><th title = "Any" style = "text-align: left;">Any</th><th title = "Any" style = "text-align: left;">Any</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: left;">1298</td><td style = "text-align: left;">0.0533542</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: left;">626</td><td style = "text-align: left;">0.0241571</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: left;">3044</td><td style = "text-align: left;">0.129214</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: left;">1988</td><td style = "text-align: left;">0.0833333</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: left;">3337</td><td style = "text-align: left;">0.141945</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001211000025</td><td style = "text-align: left;">456</td><td style = "text-align: left;">0.0167709</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001212000027</td><td style = "text-align: left;">448</td><td style = "text-align: left;">0.0164234</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001212000031</td><td style = "text-align: left;">296</td><td style = "text-align: left;">0.00981926</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001212000046</td><td style = "text-align: left;">228</td><td style = "text-align: left;">0.00686479</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001212000070</td><td style = "text-align: left;">456</td><td style = "text-align: left;">0.0167709</td></tr></tbody></table></div>

## 27.　対数変換

---
> J-061: レシート明細データ（df_receipt）の売上金額（amount）を顧客ID（customer_id）ごとに合計し、売上金額合計を常用対数化（底10）して顧客ID、売上金額合計とともに10件表示せよ。ただし、顧客IDが"Z"から始まるのものは非会員を表すため、除外して計算すること。

```julia
gd = groupby(filter(:customer_id => x -> !occursin(r"^Z", x), df_receipt), :customer_id)
df = combine(gd, :amount => sum => :sum_amount)
df.log_amount = log10.(df.sum_amount)
df2 = sort(df, :customer_id)
first(df2, 10)
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sum_amount</th><th style = "text-align: left;">log_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: right;">1298</td><td style = "text-align: right;">3.11327</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">626</td><td style = "text-align: right;">2.79657</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">3044</td><td style = "text-align: right;">3.48344</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: right;">1988</td><td style = "text-align: right;">3.29842</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">3337</td><td style = "text-align: right;">3.52336</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001211000025</td><td style = "text-align: right;">456</td><td style = "text-align: right;">2.65896</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001212000027</td><td style = "text-align: right;">448</td><td style = "text-align: right;">2.65128</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001212000031</td><td style = "text-align: right;">296</td><td style = "text-align: right;">2.47129</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001212000046</td><td style = "text-align: right;">228</td><td style = "text-align: right;">2.35793</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001212000070</td><td style = "text-align: right;">456</td><td style = "text-align: right;">2.65896</td></tr></tbody></table></div>

```julia
# 別解
df2 = df_receipt[!, [:customer_id, :amount]] |>
    @filter(!occursin(r"^Z", _.customer_id)) |>
    DataFrame;

using FreqTables
a = freqtable(df2.customer_id, weights=df2.amount)
(min_amount, max_amount) = (minimum(a), maximum(a))
DataFrame(customer_id=names(a)[1], sum_amount=a) |>
    @mutate(log_amount = log10(_.sum_amount)) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sum_amount</th><th style = "text-align: left;">log_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: right;">1298</td><td style = "text-align: right;">3.11327</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">626</td><td style = "text-align: right;">2.79657</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">3044</td><td style = "text-align: right;">3.48344</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: right;">1988</td><td style = "text-align: right;">3.29842</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">3337</td><td style = "text-align: right;">3.52336</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001211000025</td><td style = "text-align: right;">456</td><td style = "text-align: right;">2.65896</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001212000027</td><td style = "text-align: right;">448</td><td style = "text-align: right;">2.65128</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001212000031</td><td style = "text-align: right;">296</td><td style = "text-align: right;">2.47129</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001212000046</td><td style = "text-align: right;">228</td><td style = "text-align: right;">2.35793</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001212000070</td><td style = "text-align: right;">456</td><td style = "text-align: right;">2.65896</td></tr></tbody></table></div>

---
> J-062: レシート明細データ（df_receipt）の売上金額（amount）を顧客ID（customer_id）ごとに合計し、売上金額合計を自然対数化（底e）して顧客ID、売上金額合計とともに10件表示せよ。ただし、顧客IDが"Z"から始まるのものは非会員を表すため、除外して計算すること。

```julia
gd = groupby(filter(:customer_id => x -> !occursin(r"^Z", x), df_receipt), :customer_id)
df = combine(gd, :amount => sum => :sum_amount)
df.log_amount = log.(df.sum_amount)
df2 = sort(df, :customer_id)
first(df2, 10)
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sum_amount</th><th style = "text-align: left;">log_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: right;">1298</td><td style = "text-align: right;">7.16858</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">626</td><td style = "text-align: right;">6.43935</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">3044</td><td style = "text-align: right;">8.02093</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: right;">1988</td><td style = "text-align: right;">7.59488</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">3337</td><td style = "text-align: right;">8.11283</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001211000025</td><td style = "text-align: right;">456</td><td style = "text-align: right;">6.12249</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001212000027</td><td style = "text-align: right;">448</td><td style = "text-align: right;">6.10479</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001212000031</td><td style = "text-align: right;">296</td><td style = "text-align: right;">5.69036</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001212000046</td><td style = "text-align: right;">228</td><td style = "text-align: right;">5.42935</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001212000070</td><td style = "text-align: right;">456</td><td style = "text-align: right;">6.12249</td></tr></tbody></table></div>

```julia
# 別解
df2 = df_receipt[!, [:customer_id, :amount]] |>
    @filter(!occursin(r"^Z", _.customer_id)) |>
    DataFrame;

using FreqTables
a = freqtable(df2.customer_id, weights=df2.amount)
(min_amount, max_amount) = (minimum(a), maximum(a))
DataFrame(customer_id=names(a)[1], sum_amount=a) |>
    @mutate(log_amount = log(_.sum_amount)) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sum_amount</th><th style = "text-align: left;">log_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: right;">1298</td><td style = "text-align: right;">7.16858</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">626</td><td style = "text-align: right;">6.43935</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">3044</td><td style = "text-align: right;">8.02093</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: right;">1988</td><td style = "text-align: right;">7.59488</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">3337</td><td style = "text-align: right;">8.11283</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001211000025</td><td style = "text-align: right;">456</td><td style = "text-align: right;">6.12249</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001212000027</td><td style = "text-align: right;">448</td><td style = "text-align: right;">6.10479</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001212000031</td><td style = "text-align: right;">296</td><td style = "text-align: right;">5.69036</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001212000046</td><td style = "text-align: right;">228</td><td style = "text-align: right;">5.42935</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001212000070</td><td style = "text-align: right;">456</td><td style = "text-align: right;">6.12249</td></tr></tbody></table></div>

# 欠損値処理

---
> J-063: 商品データ（df_product）の単価（unit_price）と原価（unit_cost）から各商品の利益額を算出し、結果を10件表示せよ。

```julia
df = copy(df_product)  # copy() しないと，元のデータフレームが編集される
df.unit_profit = df.unit_price - df.unit_cost
first(df, 10)
```

<div><div style = "float: left;"><span>10×7 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">category_major_cd</th><th style = "text-align: left;">category_medium_cd</th><th style = "text-align: left;">category_small_cd</th><th style = "text-align: left;">unit_price</th><th style = "text-align: left;">unit_cost</th><th style = "text-align: left;">unit_profit</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String" style = "text-align: left;">String</th><th title = "String" style = "text-align: left;">String</th><th title = "Union{Missing, Int64}" style = "text-align: left;">Int64?</th><th title = "Union{Missing, Int64}" style = "text-align: left;">Int64?</th><th title = "Union{Missing, Int64}" style = "text-align: left;">Int64?</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">P040101001</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">198</td><td style = "text-align: right;">149</td><td style = "text-align: right;">49</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">P040101002</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">218</td><td style = "text-align: right;">164</td><td style = "text-align: right;">54</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">P040101003</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">230</td><td style = "text-align: right;">173</td><td style = "text-align: right;">57</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">P040101004</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">248</td><td style = "text-align: right;">186</td><td style = "text-align: right;">62</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">P040101005</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">268</td><td style = "text-align: right;">201</td><td style = "text-align: right;">67</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">P040101006</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">298</td><td style = "text-align: right;">224</td><td style = "text-align: right;">74</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">P040101007</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">338</td><td style = "text-align: right;">254</td><td style = "text-align: right;">84</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">P040101008</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">420</td><td style = "text-align: right;">315</td><td style = "text-align: right;">105</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">P040101009</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">498</td><td style = "text-align: right;">374</td><td style = "text-align: right;">124</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">P040101010</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">580</td><td style = "text-align: right;">435</td><td style = "text-align: right;">145</td></tr></tbody></table></div>

```julia
# 別解
df_product |>
    @mutate(unit_profit = _.unit_price - _.unit_cost) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×7 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">category_major_cd</th><th style = "text-align: left;">category_medium_cd</th><th style = "text-align: left;">category_small_cd</th><th style = "text-align: left;">unit_price</th><th style = "text-align: left;">unit_cost</th><th style = "text-align: left;">unit_profit</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String" style = "text-align: left;">String</th><th title = "String" style = "text-align: left;">String</th><th title = "Union{Missing, Int64}" style = "text-align: left;">Int64?</th><th title = "Union{Missing, Int64}" style = "text-align: left;">Int64?</th><th title = "Union{Missing, Int64}" style = "text-align: left;">Int64?</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">P040101001</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">198</td><td style = "text-align: right;">149</td><td style = "text-align: right;">49</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">P040101002</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">218</td><td style = "text-align: right;">164</td><td style = "text-align: right;">54</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">P040101003</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">230</td><td style = "text-align: right;">173</td><td style = "text-align: right;">57</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">P040101004</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">248</td><td style = "text-align: right;">186</td><td style = "text-align: right;">62</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">P040101005</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">268</td><td style = "text-align: right;">201</td><td style = "text-align: right;">67</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">P040101006</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">298</td><td style = "text-align: right;">224</td><td style = "text-align: right;">74</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">P040101007</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">338</td><td style = "text-align: right;">254</td><td style = "text-align: right;">84</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">P040101008</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">420</td><td style = "text-align: right;">315</td><td style = "text-align: right;">105</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">P040101009</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">498</td><td style = "text-align: right;">374</td><td style = "text-align: right;">124</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">P040101010</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">580</td><td style = "text-align: right;">435</td><td style = "text-align: right;">145</td></tr></tbody></table></div>

---
> J-064: 商品データ（df_product）の単価（unit_price）と原価（unit_cost）から、各商品の利益率の全体平均を算出せよ。ただし、単価と原価には欠損が生じていることに注意せよ。

```julia
mean(skipmissing(1 .- df_product.unit_cost ./ df_product.unit_price))
```

    0.24911389885176904

```julia
# 別解
df = df_product |>
    @mutate(unit_profit_rate = 1 - _.unit_cost / _.unit_price) |>
    DataFrame;
mean(skipmissing(df.unit_profit_rate))
```

    0.24911389885176904

```julia
# 別解2
df = df_product |>
    @mutate(unit_profit_rate = 1 - _.unit_cost / _.unit_price) |>
    @dropna(:unit_profit_rate) |>
    DataFrame;
mean(df.unit_profit_rate)
```

    0.24911389885177007

---
> J-065: 商品データ（df_product）の各商品について、利益率が30%となる新たな単価を求めよ。ただし、1円未満は切り捨てること。そして結果を10件表示させ、利益率がおよそ30％付近であることを確認せよ。ただし、単価（unit_price）と原価（unit_cost）には欠損が生じていることに注意せよ。

```julia
df = dropmissing(df_product[!, [:product_cd, :unit_price, :unit_cost]])
df.new_price = @. trunc(Int, df.unit_cost / 0.7)
df.new_profit_rate = @. 1 - df.unit_cost / df.new_price
first(df, 10)
```

<div><div style = "float: left;"><span>10×5 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">unit_price</th><th style = "text-align: left;">unit_cost</th><th style = "text-align: left;">new_price</th><th style = "text-align: left;">new_profit_rate</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">P040101001</td><td style = "text-align: right;">198</td><td style = "text-align: right;">149</td><td style = "text-align: right;">212</td><td style = "text-align: right;">0.29717</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">P040101002</td><td style = "text-align: right;">218</td><td style = "text-align: right;">164</td><td style = "text-align: right;">234</td><td style = "text-align: right;">0.299145</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">P040101003</td><td style = "text-align: right;">230</td><td style = "text-align: right;">173</td><td style = "text-align: right;">247</td><td style = "text-align: right;">0.299595</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">P040101004</td><td style = "text-align: right;">248</td><td style = "text-align: right;">186</td><td style = "text-align: right;">265</td><td style = "text-align: right;">0.298113</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">P040101005</td><td style = "text-align: right;">268</td><td style = "text-align: right;">201</td><td style = "text-align: right;">287</td><td style = "text-align: right;">0.299652</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">P040101006</td><td style = "text-align: right;">298</td><td style = "text-align: right;">224</td><td style = "text-align: right;">320</td><td style = "text-align: right;">0.3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">P040101007</td><td style = "text-align: right;">338</td><td style = "text-align: right;">254</td><td style = "text-align: right;">362</td><td style = "text-align: right;">0.298343</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">P040101008</td><td style = "text-align: right;">420</td><td style = "text-align: right;">315</td><td style = "text-align: right;">450</td><td style = "text-align: right;">0.3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">P040101009</td><td style = "text-align: right;">498</td><td style = "text-align: right;">374</td><td style = "text-align: right;">534</td><td style = "text-align: right;">0.299625</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">P040101010</td><td style = "text-align: right;">580</td><td style = "text-align: right;">435</td><td style = "text-align: right;">621</td><td style = "text-align: right;">0.299517</td></tr></tbody></table></div>

```julia
# 別解
df_product[!, [:product_cd, :unit_price, :unit_cost]] |>
    @dropna() |>
    @mutate(new_price = trunc(Int, _.unit_cost / 0.7)) |>
    @mutate(new_profit_rate = 1 - _.unit_cost / _.new_price) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×5 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">unit_price</th><th style = "text-align: left;">unit_cost</th><th style = "text-align: left;">new_price</th><th style = "text-align: left;">new_profit_rate</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">P040101001</td><td style = "text-align: right;">198</td><td style = "text-align: right;">149</td><td style = "text-align: right;">212</td><td style = "text-align: right;">0.29717</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">P040101002</td><td style = "text-align: right;">218</td><td style = "text-align: right;">164</td><td style = "text-align: right;">234</td><td style = "text-align: right;">0.299145</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">P040101003</td><td style = "text-align: right;">230</td><td style = "text-align: right;">173</td><td style = "text-align: right;">247</td><td style = "text-align: right;">0.299595</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">P040101004</td><td style = "text-align: right;">248</td><td style = "text-align: right;">186</td><td style = "text-align: right;">265</td><td style = "text-align: right;">0.298113</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">P040101005</td><td style = "text-align: right;">268</td><td style = "text-align: right;">201</td><td style = "text-align: right;">287</td><td style = "text-align: right;">0.299652</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">P040101006</td><td style = "text-align: right;">298</td><td style = "text-align: right;">224</td><td style = "text-align: right;">320</td><td style = "text-align: right;">0.3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">P040101007</td><td style = "text-align: right;">338</td><td style = "text-align: right;">254</td><td style = "text-align: right;">362</td><td style = "text-align: right;">0.298343</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">P040101008</td><td style = "text-align: right;">420</td><td style = "text-align: right;">315</td><td style = "text-align: right;">450</td><td style = "text-align: right;">0.3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">P040101009</td><td style = "text-align: right;">498</td><td style = "text-align: right;">374</td><td style = "text-align: right;">534</td><td style = "text-align: right;">0.299625</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">P040101010</td><td style = "text-align: right;">580</td><td style = "text-align: right;">435</td><td style = "text-align: right;">621</td><td style = "text-align: right;">0.299517</td></tr></tbody></table></div>

---
> J-066: 商品データ（df_product）の各商品について、利益率が30%となる新たな単価を求めよ。今回は、1円未満を丸めること（四捨五入または偶数への丸めで良い）。そして結果を10件表示させ、利益率がおよそ30％付近であることを確認せよ。ただし、単価（unit_price）と原価（unit_cost）には欠損が生じていることに注意せよ。

```julia
df = dropmissing(df_product[!, [:product_cd, :unit_price, :unit_cost]])
df.new_price = @. round(Int, df.unit_cost / 0.7)
df.new_profit_rate = @. 1 - df.unit_cost / df.new_price
first(df, 10)
```

<div><div style = "float: left;"><span>10×5 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">unit_price</th><th style = "text-align: left;">unit_cost</th><th style = "text-align: left;">new_price</th><th style = "text-align: left;">new_profit_rate</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">P040101001</td><td style = "text-align: right;">198</td><td style = "text-align: right;">149</td><td style = "text-align: right;">213</td><td style = "text-align: right;">0.300469</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">P040101002</td><td style = "text-align: right;">218</td><td style = "text-align: right;">164</td><td style = "text-align: right;">234</td><td style = "text-align: right;">0.299145</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">P040101003</td><td style = "text-align: right;">230</td><td style = "text-align: right;">173</td><td style = "text-align: right;">247</td><td style = "text-align: right;">0.299595</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">P040101004</td><td style = "text-align: right;">248</td><td style = "text-align: right;">186</td><td style = "text-align: right;">266</td><td style = "text-align: right;">0.300752</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">P040101005</td><td style = "text-align: right;">268</td><td style = "text-align: right;">201</td><td style = "text-align: right;">287</td><td style = "text-align: right;">0.299652</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">P040101006</td><td style = "text-align: right;">298</td><td style = "text-align: right;">224</td><td style = "text-align: right;">320</td><td style = "text-align: right;">0.3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">P040101007</td><td style = "text-align: right;">338</td><td style = "text-align: right;">254</td><td style = "text-align: right;">363</td><td style = "text-align: right;">0.300275</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">P040101008</td><td style = "text-align: right;">420</td><td style = "text-align: right;">315</td><td style = "text-align: right;">450</td><td style = "text-align: right;">0.3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">P040101009</td><td style = "text-align: right;">498</td><td style = "text-align: right;">374</td><td style = "text-align: right;">534</td><td style = "text-align: right;">0.299625</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">P040101010</td><td style = "text-align: right;">580</td><td style = "text-align: right;">435</td><td style = "text-align: right;">621</td><td style = "text-align: right;">0.299517</td></tr></tbody></table></div>

```julia
# 別解
df_product[!, [:product_cd, :unit_price, :unit_cost]] |>
    @dropna() |>
    @mutate(new_price = round(Int, _.unit_cost / 0.7)) |>
    @mutate(new_profit_rate = 1 - _.unit_cost / _.new_price) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×5 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">unit_price</th><th style = "text-align: left;">unit_cost</th><th style = "text-align: left;">new_price</th><th style = "text-align: left;">new_profit_rate</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">P040101001</td><td style = "text-align: right;">198</td><td style = "text-align: right;">149</td><td style = "text-align: right;">213</td><td style = "text-align: right;">0.300469</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">P040101002</td><td style = "text-align: right;">218</td><td style = "text-align: right;">164</td><td style = "text-align: right;">234</td><td style = "text-align: right;">0.299145</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">P040101003</td><td style = "text-align: right;">230</td><td style = "text-align: right;">173</td><td style = "text-align: right;">247</td><td style = "text-align: right;">0.299595</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">P040101004</td><td style = "text-align: right;">248</td><td style = "text-align: right;">186</td><td style = "text-align: right;">266</td><td style = "text-align: right;">0.300752</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">P040101005</td><td style = "text-align: right;">268</td><td style = "text-align: right;">201</td><td style = "text-align: right;">287</td><td style = "text-align: right;">0.299652</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">P040101006</td><td style = "text-align: right;">298</td><td style = "text-align: right;">224</td><td style = "text-align: right;">320</td><td style = "text-align: right;">0.3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">P040101007</td><td style = "text-align: right;">338</td><td style = "text-align: right;">254</td><td style = "text-align: right;">363</td><td style = "text-align: right;">0.300275</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">P040101008</td><td style = "text-align: right;">420</td><td style = "text-align: right;">315</td><td style = "text-align: right;">450</td><td style = "text-align: right;">0.3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">P040101009</td><td style = "text-align: right;">498</td><td style = "text-align: right;">374</td><td style = "text-align: right;">534</td><td style = "text-align: right;">0.299625</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">P040101010</td><td style = "text-align: right;">580</td><td style = "text-align: right;">435</td><td style = "text-align: right;">621</td><td style = "text-align: right;">0.299517</td></tr></tbody></table></div>

---
> J-067: 商品データ（df_product）の各商品について、利益率が30%となる新たな単価を求めよ。今回は、1円未満を切り上げること。そして結果を10件表示させ、利益率がおよそ30％付近であることを確認せよ。ただし、単価（unit_price）と原価（unit_cost）には欠損が生じていることに注意せよ。

```julia
df = dropmissing(df_product[!, [:product_cd, :unit_price, :unit_cost]])
df.new_price = @. ceil(Int, df.unit_cost / 0.7)
df.new_profit_rate = @. 1 - df.unit_cost / df.new_price
first(df, 10)
```

<div><div style = "float: left;"><span>10×5 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">unit_price</th><th style = "text-align: left;">unit_cost</th><th style = "text-align: left;">new_price</th><th style = "text-align: left;">new_profit_rate</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">P040101001</td><td style = "text-align: right;">198</td><td style = "text-align: right;">149</td><td style = "text-align: right;">213</td><td style = "text-align: right;">0.300469</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">P040101002</td><td style = "text-align: right;">218</td><td style = "text-align: right;">164</td><td style = "text-align: right;">235</td><td style = "text-align: right;">0.302128</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">P040101003</td><td style = "text-align: right;">230</td><td style = "text-align: right;">173</td><td style = "text-align: right;">248</td><td style = "text-align: right;">0.302419</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">P040101004</td><td style = "text-align: right;">248</td><td style = "text-align: right;">186</td><td style = "text-align: right;">266</td><td style = "text-align: right;">0.300752</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">P040101005</td><td style = "text-align: right;">268</td><td style = "text-align: right;">201</td><td style = "text-align: right;">288</td><td style = "text-align: right;">0.302083</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">P040101006</td><td style = "text-align: right;">298</td><td style = "text-align: right;">224</td><td style = "text-align: right;">320</td><td style = "text-align: right;">0.3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">P040101007</td><td style = "text-align: right;">338</td><td style = "text-align: right;">254</td><td style = "text-align: right;">363</td><td style = "text-align: right;">0.300275</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">P040101008</td><td style = "text-align: right;">420</td><td style = "text-align: right;">315</td><td style = "text-align: right;">451</td><td style = "text-align: right;">0.301552</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">P040101009</td><td style = "text-align: right;">498</td><td style = "text-align: right;">374</td><td style = "text-align: right;">535</td><td style = "text-align: right;">0.300935</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">P040101010</td><td style = "text-align: right;">580</td><td style = "text-align: right;">435</td><td style = "text-align: right;">622</td><td style = "text-align: right;">0.300643</td></tr></tbody></table></div>

```julia
df_product[!, [:product_cd, :unit_price, :unit_cost]] |>
    @dropna() |>
    @mutate(new_price = ceil(Int, _.unit_cost / 0.7)) |>
    @mutate(new_profit_rate = 1 - _.unit_cost / _.new_price) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×5 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">unit_price</th><th style = "text-align: left;">unit_cost</th><th style = "text-align: left;">new_price</th><th style = "text-align: left;">new_profit_rate</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">P040101001</td><td style = "text-align: right;">198</td><td style = "text-align: right;">149</td><td style = "text-align: right;">213</td><td style = "text-align: right;">0.300469</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">P040101002</td><td style = "text-align: right;">218</td><td style = "text-align: right;">164</td><td style = "text-align: right;">235</td><td style = "text-align: right;">0.302128</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">P040101003</td><td style = "text-align: right;">230</td><td style = "text-align: right;">173</td><td style = "text-align: right;">248</td><td style = "text-align: right;">0.302419</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">P040101004</td><td style = "text-align: right;">248</td><td style = "text-align: right;">186</td><td style = "text-align: right;">266</td><td style = "text-align: right;">0.300752</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">P040101005</td><td style = "text-align: right;">268</td><td style = "text-align: right;">201</td><td style = "text-align: right;">288</td><td style = "text-align: right;">0.302083</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">P040101006</td><td style = "text-align: right;">298</td><td style = "text-align: right;">224</td><td style = "text-align: right;">320</td><td style = "text-align: right;">0.3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">P040101007</td><td style = "text-align: right;">338</td><td style = "text-align: right;">254</td><td style = "text-align: right;">363</td><td style = "text-align: right;">0.300275</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">P040101008</td><td style = "text-align: right;">420</td><td style = "text-align: right;">315</td><td style = "text-align: right;">451</td><td style = "text-align: right;">0.301552</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">P040101009</td><td style = "text-align: right;">498</td><td style = "text-align: right;">374</td><td style = "text-align: right;">535</td><td style = "text-align: right;">0.300935</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">P040101010</td><td style = "text-align: right;">580</td><td style = "text-align: right;">435</td><td style = "text-align: right;">622</td><td style = "text-align: right;">0.300643</td></tr></tbody></table></div>

---
> J-068: 商品データ（df_product）の各商品について、消費税率10％の税込み金額を求めよ。1円未満の端数は切り捨てとし、結果を10件表示せよ。ただし、単価（unit_price）には欠損が生じていることに注意せよ。

```julia
df = dropmissing(df_product[!, [:product_cd, :unit_price]])
df.tax_price = @. trunc(Int, df.unit_price * 1.1)
first(df, 10)
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">unit_price</th><th style = "text-align: left;">tax_price</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">P040101001</td><td style = "text-align: right;">198</td><td style = "text-align: right;">217</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">P040101002</td><td style = "text-align: right;">218</td><td style = "text-align: right;">239</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">P040101003</td><td style = "text-align: right;">230</td><td style = "text-align: right;">253</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">P040101004</td><td style = "text-align: right;">248</td><td style = "text-align: right;">272</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">P040101005</td><td style = "text-align: right;">268</td><td style = "text-align: right;">294</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">P040101006</td><td style = "text-align: right;">298</td><td style = "text-align: right;">327</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">P040101007</td><td style = "text-align: right;">338</td><td style = "text-align: right;">371</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">P040101008</td><td style = "text-align: right;">420</td><td style = "text-align: right;">462</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">P040101009</td><td style = "text-align: right;">498</td><td style = "text-align: right;">547</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">P040101010</td><td style = "text-align: right;">580</td><td style = "text-align: right;">638</td></tr></tbody></table></div>

```julia
# 別解
df_product[!, [:product_cd, :unit_price]] |>
    @dropna() |>
    @mutate(tax_price = trunc(Int, _.unit_price * 1.1)) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">unit_price</th><th style = "text-align: left;">tax_price</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">P040101001</td><td style = "text-align: right;">198</td><td style = "text-align: right;">217</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">P040101002</td><td style = "text-align: right;">218</td><td style = "text-align: right;">239</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">P040101003</td><td style = "text-align: right;">230</td><td style = "text-align: right;">253</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">P040101004</td><td style = "text-align: right;">248</td><td style = "text-align: right;">272</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">P040101005</td><td style = "text-align: right;">268</td><td style = "text-align: right;">294</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">P040101006</td><td style = "text-align: right;">298</td><td style = "text-align: right;">327</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">P040101007</td><td style = "text-align: right;">338</td><td style = "text-align: right;">371</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">P040101008</td><td style = "text-align: right;">420</td><td style = "text-align: right;">462</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">P040101009</td><td style = "text-align: right;">498</td><td style = "text-align: right;">547</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">P040101010</td><td style = "text-align: right;">580</td><td style = "text-align: right;">638</td></tr></tbody></table></div>

---
> J-069: レシート明細データ（df_receipt）と商品データ（df_product）を結合し、顧客毎に全商品の売上金額合計と、カテゴリ大区分コード（category_major_cd）が"07"（瓶詰缶詰）の売上金額合計を計算の上、両者の比率を求めよ。抽出対象はカテゴリ大区分コード"07"（瓶詰缶詰）の売上実績がある顧客のみとし、結果を10件表示せよ。

```julia
df = innerjoin(df_receipt[!, [:customer_id, :product_cd, :amount]],
               df_product[!, [:category_major_cd, :product_cd]],
               on=:product_cd, makeunique=true)
df1 = combine(groupby(df, :customer_id), :amount => sum => :sum_all)

filter!(:category_major_cd => x -> x == "07", df)
df2 = combine(groupby(df, :customer_id), :amount => sum => :sum_07)

df3 = innerjoin(df1, df2, on=:customer_id)
df3.sales_rate = df3.sum_07 ./ df3.sum_all
sort!(df3, :customer_id)
first(df3, 10)
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sum_all</th><th style = "text-align: left;">sum_07</th><th style = "text-align: left;">sales_rate</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: right;">1298</td><td style = "text-align: right;">1298</td><td style = "text-align: right;">1.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">626</td><td style = "text-align: right;">486</td><td style = "text-align: right;">0.776358</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">3044</td><td style = "text-align: right;">2694</td><td style = "text-align: right;">0.88502</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: right;">1988</td><td style = "text-align: right;">346</td><td style = "text-align: right;">0.174044</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">3337</td><td style = "text-align: right;">2004</td><td style = "text-align: right;">0.600539</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001212000027</td><td style = "text-align: right;">448</td><td style = "text-align: right;">200</td><td style = "text-align: right;">0.446429</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001212000031</td><td style = "text-align: right;">296</td><td style = "text-align: right;">296</td><td style = "text-align: right;">1.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001212000046</td><td style = "text-align: right;">228</td><td style = "text-align: right;">108</td><td style = "text-align: right;">0.473684</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001212000070</td><td style = "text-align: right;">456</td><td style = "text-align: right;">308</td><td style = "text-align: right;">0.675439</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001213000018</td><td style = "text-align: right;">243</td><td style = "text-align: right;">145</td><td style = "text-align: right;">0.596708</td></tr></tbody></table></div>

```julia
# 別解
df1 = df_receipt |>
    @select(:customer_id, :product_cd, :amount) |> DataFrame;

df2 = df_product |>
    @select(:category_major_cd, :product_cd) |> DataFrame;

df3 = df1 |>
    @join(df2, _.product_cd, _.product_cd, {_.customer_id, _.amount, __.category_major_cd}) |>
    DataFrame;

df4 = df3 |>
    @groupby(_.customer_id) |>
    @map({customer_id = key(_), sum_all = sum(_.amount)}) |>
    DataFrame;

df5 = df3 |>
    @filter(_.category_major_cd == "07") |>
    @groupby(_.customer_id) |>
    @map({customer_id = key(_), sum_07 = sum(_.amount)}) |>
    DataFrame;

df4 |>
    @join(df5, _.customer_id, _.customer_id, {_.customer_id, _.sum_all, __.sum_07}) |>
    @mutate(sales_rate = _.sum_07 / _.sum_all) |>
    @orderby(_.customer_id) |>
    @take(10) |> DataFrame
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sum_all</th><th style = "text-align: left;">sum_07</th><th style = "text-align: left;">sales_rate</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: right;">1298</td><td style = "text-align: right;">1298</td><td style = "text-align: right;">1.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">626</td><td style = "text-align: right;">486</td><td style = "text-align: right;">0.776358</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">3044</td><td style = "text-align: right;">2694</td><td style = "text-align: right;">0.88502</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: right;">1988</td><td style = "text-align: right;">346</td><td style = "text-align: right;">0.174044</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">3337</td><td style = "text-align: right;">2004</td><td style = "text-align: right;">0.600539</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001212000027</td><td style = "text-align: right;">448</td><td style = "text-align: right;">200</td><td style = "text-align: right;">0.446429</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001212000031</td><td style = "text-align: right;">296</td><td style = "text-align: right;">296</td><td style = "text-align: right;">1.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001212000046</td><td style = "text-align: right;">228</td><td style = "text-align: right;">108</td><td style = "text-align: right;">0.473684</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001212000070</td><td style = "text-align: right;">456</td><td style = "text-align: right;">308</td><td style = "text-align: right;">0.675439</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001213000018</td><td style = "text-align: right;">243</td><td style = "text-align: right;">145</td><td style = "text-align: right;">0.596708</td></tr></tbody></table></div>

# 経過日数，月数，年数

---
> J-070: レシート明細データ（df_receipt）の売上日（sales_ymd）に対し、顧客データ（df_customer）の会員申込日（application_date）からの経過日数を計算し、顧客ID（customer_id）、売上日、会員申込日とともに10件表示せよ（sales_ymdは数値、application_dateは文字列でデータを保持している点に注意）。

```julia
using Dates
date(d) = @. Date(d ÷ 10000, (d % 10000) ÷ 100, d % 100)
df = innerjoin(df_receipt[!, [:customer_id, :sales_ymd]],
          df_customer[!, [:customer_id, :application_date]],
          on=:customer_id);
df.elapsed_days = @. Dates.value(date(df.sales_ymd) - date(parse(Int, df.application_date)))
first(df, 10)
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">elapsed_days</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String" style = "text-align: left;">String</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS006214000001</td><td style = "text-align: right;">20181103</td><td style = "text-align: left;">20150201</td><td style = "text-align: right;">1371</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS008415000097</td><td style = "text-align: right;">20181118</td><td style = "text-align: left;">20150322</td><td style = "text-align: right;">1337</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS028414000014</td><td style = "text-align: right;">20170712</td><td style = "text-align: left;">20150711</td><td style = "text-align: right;">732</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS025415000050</td><td style = "text-align: right;">20180821</td><td style = "text-align: left;">20160131</td><td style = "text-align: right;">933</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS003515000195</td><td style = "text-align: right;">20190605</td><td style = "text-align: left;">20150306</td><td style = "text-align: right;">1552</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS024514000042</td><td style = "text-align: right;">20181205</td><td style = "text-align: left;">20151010</td><td style = "text-align: right;">1152</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS040415000178</td><td style = "text-align: right;">20190922</td><td style = "text-align: left;">20150627</td><td style = "text-align: right;">1548</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS027514000015</td><td style = "text-align: right;">20191010</td><td style = "text-align: left;">20151101</td><td style = "text-align: right;">1439</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS025415000134</td><td style = "text-align: right;">20190918</td><td style = "text-align: left;">20150720</td><td style = "text-align: right;">1521</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS021515000126</td><td style = "text-align: right;">20171010</td><td style = "text-align: left;">20150508</td><td style = "text-align: right;">886</td></tr></tbody></table></div>

```julia
# 別解
using Dates
date(d) = @. Date(d ÷ 10000, (d % 10000) ÷ 100, d % 100)
df_receipt |>
    @join(df_customer, _.customer_id, _.customer_id, {_.customer_id, _.sales_ymd, __.application_date}) |>
    @mutate(elapsed_days = Dates.value(date(_.sales_ymd) - date(parse(Int, _.application_date)))) |>
    @take(10) |> DataFrame
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">elapsed_days</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String" style = "text-align: left;">String</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS006214000001</td><td style = "text-align: right;">20181103</td><td style = "text-align: left;">20150201</td><td style = "text-align: right;">1371</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS008415000097</td><td style = "text-align: right;">20181118</td><td style = "text-align: left;">20150322</td><td style = "text-align: right;">1337</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS028414000014</td><td style = "text-align: right;">20170712</td><td style = "text-align: left;">20150711</td><td style = "text-align: right;">732</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS025415000050</td><td style = "text-align: right;">20180821</td><td style = "text-align: left;">20160131</td><td style = "text-align: right;">933</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS003515000195</td><td style = "text-align: right;">20190605</td><td style = "text-align: left;">20150306</td><td style = "text-align: right;">1552</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS024514000042</td><td style = "text-align: right;">20181205</td><td style = "text-align: left;">20151010</td><td style = "text-align: right;">1152</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS040415000178</td><td style = "text-align: right;">20190922</td><td style = "text-align: left;">20150627</td><td style = "text-align: right;">1548</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS027514000015</td><td style = "text-align: right;">20191010</td><td style = "text-align: left;">20151101</td><td style = "text-align: right;">1439</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS025415000134</td><td style = "text-align: right;">20190918</td><td style = "text-align: left;">20150720</td><td style = "text-align: right;">1521</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS021515000126</td><td style = "text-align: right;">20171010</td><td style = "text-align: left;">20150508</td><td style = "text-align: right;">886</td></tr></tbody></table></div>

---
> J-071: レシート明細データ（df_receipt）の売上日（sales_ymd）に対し、顧客データ（df_customer）の会員申込日（application_date）からの経過月数を計算し、顧客ID（customer_id）、売上日、会員申込日とともに10件表示せよ（sales_ymdは数値、application_dateは文字列でデータを保持している点に注意）。1ヶ月未満は切り捨てること。

```julia
using Dates
date2(d) = @. (d ÷ 10000, (d % 10000) ÷ 100, d % 100)
function date3(d_begin, d_end)
    y, m, d = date2(d_end) .- date2(parse(Int, d_begin))
    12y + m + (d < 0)*(-1)
end;
```

```julia
df = innerjoin(df_receipt[!, [:customer_id, :sales_ymd]],
          df_customer[!, [:customer_id, :application_date]],
          on=:customer_id);
df.elapsed_months = date3.(df.application_date, df.sales_ymd)
first(df, 10)
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">elapsed_months</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String" style = "text-align: left;">String</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS006214000001</td><td style = "text-align: right;">20181103</td><td style = "text-align: left;">20150201</td><td style = "text-align: right;">45</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS008415000097</td><td style = "text-align: right;">20181118</td><td style = "text-align: left;">20150322</td><td style = "text-align: right;">43</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS028414000014</td><td style = "text-align: right;">20170712</td><td style = "text-align: left;">20150711</td><td style = "text-align: right;">24</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS025415000050</td><td style = "text-align: right;">20180821</td><td style = "text-align: left;">20160131</td><td style = "text-align: right;">30</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS003515000195</td><td style = "text-align: right;">20190605</td><td style = "text-align: left;">20150306</td><td style = "text-align: right;">50</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS024514000042</td><td style = "text-align: right;">20181205</td><td style = "text-align: left;">20151010</td><td style = "text-align: right;">37</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS040415000178</td><td style = "text-align: right;">20190922</td><td style = "text-align: left;">20150627</td><td style = "text-align: right;">50</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS027514000015</td><td style = "text-align: right;">20191010</td><td style = "text-align: left;">20151101</td><td style = "text-align: right;">47</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS025415000134</td><td style = "text-align: right;">20190918</td><td style = "text-align: left;">20150720</td><td style = "text-align: right;">49</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS021515000126</td><td style = "text-align: right;">20171010</td><td style = "text-align: left;">20150508</td><td style = "text-align: right;">29</td></tr></tbody></table></div>

```julia
# 別解
df_receipt |>
    @join(df_customer, _.customer_id, _.customer_id, {_.customer_id, _.sales_ymd, __.application_date}) |>
    @mutate(elapsed_months = date3( _.application_date, _.sales_ymd)) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">elapsed_months</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String" style = "text-align: left;">String</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS006214000001</td><td style = "text-align: right;">20181103</td><td style = "text-align: left;">20150201</td><td style = "text-align: right;">45</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS008415000097</td><td style = "text-align: right;">20181118</td><td style = "text-align: left;">20150322</td><td style = "text-align: right;">43</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS028414000014</td><td style = "text-align: right;">20170712</td><td style = "text-align: left;">20150711</td><td style = "text-align: right;">24</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS025415000050</td><td style = "text-align: right;">20180821</td><td style = "text-align: left;">20160131</td><td style = "text-align: right;">30</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS003515000195</td><td style = "text-align: right;">20190605</td><td style = "text-align: left;">20150306</td><td style = "text-align: right;">50</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS024514000042</td><td style = "text-align: right;">20181205</td><td style = "text-align: left;">20151010</td><td style = "text-align: right;">37</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS040415000178</td><td style = "text-align: right;">20190922</td><td style = "text-align: left;">20150627</td><td style = "text-align: right;">50</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS027514000015</td><td style = "text-align: right;">20191010</td><td style = "text-align: left;">20151101</td><td style = "text-align: right;">47</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS025415000134</td><td style = "text-align: right;">20190918</td><td style = "text-align: left;">20150720</td><td style = "text-align: right;">49</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS021515000126</td><td style = "text-align: right;">20171010</td><td style = "text-align: left;">20150508</td><td style = "text-align: right;">29</td></tr></tbody></table></div>

---
> J-072: レシート明細データ（df_receipt）の売上日（df_customer）に対し、顧客データ（df_customer）の会員申込日（application_date）からの経過年数を計算し、顧客ID（customer_id）、売上日、会員申込日とともに10件表示せよ（sales_ymdは数値、application_dateは文字列でデータを保持している点に注意）。1年未満は切り捨てること。

```julia
year2(ymd, ymd2) = (ymd2 .- ymd) .÷ 10000
df = innerjoin(df_receipt[!, [:customer_id, :sales_ymd]],
          df_customer[!, [:customer_id, :application_date]],
          on=:customer_id)
df.elapsed_years = year2(parse.(Int, df.application_date), df.sales_ymd)
first(df, 10)
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">elapsed_years</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String" style = "text-align: left;">String</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS006214000001</td><td style = "text-align: right;">20181103</td><td style = "text-align: left;">20150201</td><td style = "text-align: right;">3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS008415000097</td><td style = "text-align: right;">20181118</td><td style = "text-align: left;">20150322</td><td style = "text-align: right;">3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS028414000014</td><td style = "text-align: right;">20170712</td><td style = "text-align: left;">20150711</td><td style = "text-align: right;">2</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS025415000050</td><td style = "text-align: right;">20180821</td><td style = "text-align: left;">20160131</td><td style = "text-align: right;">2</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS003515000195</td><td style = "text-align: right;">20190605</td><td style = "text-align: left;">20150306</td><td style = "text-align: right;">4</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS024514000042</td><td style = "text-align: right;">20181205</td><td style = "text-align: left;">20151010</td><td style = "text-align: right;">3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS040415000178</td><td style = "text-align: right;">20190922</td><td style = "text-align: left;">20150627</td><td style = "text-align: right;">4</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS027514000015</td><td style = "text-align: right;">20191010</td><td style = "text-align: left;">20151101</td><td style = "text-align: right;">3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS025415000134</td><td style = "text-align: right;">20190918</td><td style = "text-align: left;">20150720</td><td style = "text-align: right;">4</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS021515000126</td><td style = "text-align: right;">20171010</td><td style = "text-align: left;">20150508</td><td style = "text-align: right;">2</td></tr></tbody></table></div>

```julia
# 別解
year2(ymd, ymd2) = (ymd2 - ymd) ÷ 10000
df_receipt |>
    @join(df_customer, _.customer_id, _.customer_id, {_.customer_id, _.sales_ymd, __.application_date}) |>
    @mutate(elapsed_years = year2(parse(Int, _.application_date), _.sales_ymd)) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">elapsed_years</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String" style = "text-align: left;">String</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS006214000001</td><td style = "text-align: right;">20181103</td><td style = "text-align: left;">20150201</td><td style = "text-align: right;">3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS008415000097</td><td style = "text-align: right;">20181118</td><td style = "text-align: left;">20150322</td><td style = "text-align: right;">3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS028414000014</td><td style = "text-align: right;">20170712</td><td style = "text-align: left;">20150711</td><td style = "text-align: right;">2</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS025415000050</td><td style = "text-align: right;">20180821</td><td style = "text-align: left;">20160131</td><td style = "text-align: right;">2</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS003515000195</td><td style = "text-align: right;">20190605</td><td style = "text-align: left;">20150306</td><td style = "text-align: right;">4</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS024514000042</td><td style = "text-align: right;">20181205</td><td style = "text-align: left;">20151010</td><td style = "text-align: right;">3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS040415000178</td><td style = "text-align: right;">20190922</td><td style = "text-align: left;">20150627</td><td style = "text-align: right;">4</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS027514000015</td><td style = "text-align: right;">20191010</td><td style = "text-align: left;">20151101</td><td style = "text-align: right;">3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS025415000134</td><td style = "text-align: right;">20190918</td><td style = "text-align: left;">20150720</td><td style = "text-align: right;">4</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS021515000126</td><td style = "text-align: right;">20171010</td><td style = "text-align: left;">20150508</td><td style = "text-align: right;">2</td></tr></tbody></table></div>

---
> J-073: レシート明細データ（df_receipt）の売上日（sales_ymd）に対し、顧客データ（df_customer）の会員申込日（application_date）からのエポック秒による経過時間を計算し、顧客ID（customer_id）、売上日、会員申込日とともに10件表示せよ（なお、sales_ymdは数値、application_dateは文字列でデータを保持している点に注意）。なお、時間情報は保有していないため各日付は0時0分0秒を表すものとする。

```julia
datetime(d) = DateTime(d ÷ 10000, (d % 10000) ÷ 100, d % 100, 0, 0, 0)

df = innerjoin(df_receipt[!, [:customer_id, :sales_ymd]],
          df_customer[!, [:customer_id, :application_date]],
          on=:customer_id)
df.elapsed_epoch = convert.(Int64, Dates.value.(datetime.(df.sales_ymd) - datetime.(parse.(Int, df.application_date)))/1000)
first(df, 10)
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">elapsed_epoch</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String" style = "text-align: left;">String</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS006214000001</td><td style = "text-align: right;">20181103</td><td style = "text-align: left;">20150201</td><td style = "text-align: right;">118454400</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS008415000097</td><td style = "text-align: right;">20181118</td><td style = "text-align: left;">20150322</td><td style = "text-align: right;">115516800</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS028414000014</td><td style = "text-align: right;">20170712</td><td style = "text-align: left;">20150711</td><td style = "text-align: right;">63244800</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS025415000050</td><td style = "text-align: right;">20180821</td><td style = "text-align: left;">20160131</td><td style = "text-align: right;">80611200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS003515000195</td><td style = "text-align: right;">20190605</td><td style = "text-align: left;">20150306</td><td style = "text-align: right;">134092800</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS024514000042</td><td style = "text-align: right;">20181205</td><td style = "text-align: left;">20151010</td><td style = "text-align: right;">99532800</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS040415000178</td><td style = "text-align: right;">20190922</td><td style = "text-align: left;">20150627</td><td style = "text-align: right;">133747200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS027514000015</td><td style = "text-align: right;">20191010</td><td style = "text-align: left;">20151101</td><td style = "text-align: right;">124329600</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS025415000134</td><td style = "text-align: right;">20190918</td><td style = "text-align: left;">20150720</td><td style = "text-align: right;">131414400</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS021515000126</td><td style = "text-align: right;">20171010</td><td style = "text-align: left;">20150508</td><td style = "text-align: right;">76550400</td></tr></tbody></table></div>

```julia
# 別解
datetime(d) = DateTime(d ÷ 10000, (d % 10000) ÷ 100, d % 100, 0, 0, 0)

df_receipt |>
    @join(df_customer, _.customer_id, _.customer_id, {_.customer_id, _.sales_ymd, __.application_date}) |>
    @mutate(elapsed_epoch = convert(Int64, Dates.value(datetime(_.sales_ymd) .- datetime(parse(Int, _.application_date)))/1000)) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">elapsed_epoch</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String" style = "text-align: left;">String</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS006214000001</td><td style = "text-align: right;">20181103</td><td style = "text-align: left;">20150201</td><td style = "text-align: right;">118454400</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS008415000097</td><td style = "text-align: right;">20181118</td><td style = "text-align: left;">20150322</td><td style = "text-align: right;">115516800</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS028414000014</td><td style = "text-align: right;">20170712</td><td style = "text-align: left;">20150711</td><td style = "text-align: right;">63244800</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS025415000050</td><td style = "text-align: right;">20180821</td><td style = "text-align: left;">20160131</td><td style = "text-align: right;">80611200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS003515000195</td><td style = "text-align: right;">20190605</td><td style = "text-align: left;">20150306</td><td style = "text-align: right;">134092800</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS024514000042</td><td style = "text-align: right;">20181205</td><td style = "text-align: left;">20151010</td><td style = "text-align: right;">99532800</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS040415000178</td><td style = "text-align: right;">20190922</td><td style = "text-align: left;">20150627</td><td style = "text-align: right;">133747200</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS027514000015</td><td style = "text-align: right;">20191010</td><td style = "text-align: left;">20151101</td><td style = "text-align: right;">124329600</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS025415000134</td><td style = "text-align: right;">20190918</td><td style = "text-align: left;">20150720</td><td style = "text-align: right;">131414400</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS021515000126</td><td style = "text-align: right;">20171010</td><td style = "text-align: left;">20150508</td><td style = "text-align: right;">76550400</td></tr></tbody></table></div>

---
> J-074: レシート明細データ（df_receipt）の売上日（sales_ymd）に対し、当該週の月曜日からの経過日数を計算し、売上日、直前の月曜日付とともに10件表示せよ（sales_ymdは数値でデータを保持している点に注意）。

```julia
date(d) = Date(d ÷ 10000, (d % 10000) ÷ 100, d % 100)
df = copy(df_receipt)  # または，df = df_receipt[!, [:sales_ymd]]
df.monday = firstdayofweek.(date.(df.sales_ymd))
df.elapsed_days = Dates.value.(date.(df.sales_ymd) - df.monday)
df = select(df, [:sales_ymd,  :elapsed_days, :monday])
first(df, 10)
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">elapsed_days</th><th style = "text-align: left;">monday</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Date" style = "text-align: left;">Date</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20181103</td><td style = "text-align: right;">5</td><td style = "text-align: left;">2018-10-29</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20181118</td><td style = "text-align: right;">6</td><td style = "text-align: left;">2018-11-12</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170712</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2017-07-10</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20190205</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2019-02-04</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20180821</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2018-08-20</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">20190605</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2019-06-03</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">20181205</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2018-12-03</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">20190922</td><td style = "text-align: right;">6</td><td style = "text-align: left;">2019-09-16</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">20170504</td><td style = "text-align: right;">3</td><td style = "text-align: left;">2017-05-01</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">20191010</td><td style = "text-align: right;">3</td><td style = "text-align: left;">2019-10-07</td></tr></tbody></table></div>

```julia
# 別解
using Dates
date(d) = Date(d ÷ 10000, (d % 10000) ÷ 100, d % 100)
df_receipt |>
    #@select(:sales_ymd) |>
    @mutate(monday = firstdayofweek(date(_.sales_ymd))) |>
    @mutate(elapsed_days = Dates.value(date(_.sales_ymd) - _.monday)) |>
    @select(:sales_ymd,  :elapsed_days, :monday) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">sales_ymd</th><th style = "text-align: left;">elapsed_days</th><th style = "text-align: left;">monday</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Date" style = "text-align: left;">Date</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: right;">20181103</td><td style = "text-align: right;">5</td><td style = "text-align: left;">2018-10-29</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: right;">20181118</td><td style = "text-align: right;">6</td><td style = "text-align: left;">2018-11-12</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: right;">20170712</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2017-07-10</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: right;">20190205</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2019-02-04</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: right;">20180821</td><td style = "text-align: right;">1</td><td style = "text-align: left;">2018-08-20</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: right;">20190605</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2019-06-03</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: right;">20181205</td><td style = "text-align: right;">2</td><td style = "text-align: left;">2018-12-03</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: right;">20190922</td><td style = "text-align: right;">6</td><td style = "text-align: left;">2019-09-16</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: right;">20170504</td><td style = "text-align: right;">3</td><td style = "text-align: left;">2017-05-01</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: right;">20191010</td><td style = "text-align: right;">3</td><td style = "text-align: left;">2019-10-07</td></tr></tbody></table></div>

# 無作為抽出

---
> J-075: 顧客データ（df_customer）からランダムに1%のデータを抽出し、先頭から10件表示せよ。

```julia
using StatsBase
n = nrow(df_customer)
df = df_customer[sample(1:n, floor(Int, n*0.01), replace=false), :]
first(df, 10)
```

<div><div style = "float: left;"><span>10×11 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">customer_name</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">gender</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">postal_cd</th><th style = "text-align: left;">address</th><th style = "text-align: left;">application_store_cd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">status_cd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001313000265</td><td style = "text-align: left;">竹村 遥</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1984-04-13</td><td style = "text-align: right;">34</td><td style = "text-align: left;">144-0035</td><td style = "text-align: left;">東京都大田区南蒲田**********</td><td style = "text-align: left;">S13001</td><td style = "text-align: left;">20160624</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001415000572</td><td style = "text-align: left;">田口 ヒカル</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1977-07-31</td><td style = "text-align: right;">41</td><td style = "text-align: left;">144-0056</td><td style = "text-align: left;">東京都大田区西六郷**********</td><td style = "text-align: left;">S13001</td><td style = "text-align: left;">20170413</td><td style = "text-align: left;">8-20100925-7</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS013313000160</td><td style = "text-align: left;">大塚 遥</td><td style = "text-align: left;">9</td><td style = "text-align: left;">不明</td><td style = "text-align: left;">1980-09-28</td><td style = "text-align: right;">38</td><td style = "text-align: left;">275-0022</td><td style = "text-align: left;">千葉県習志野市香澄**********</td><td style = "text-align: left;">S12013</td><td style = "text-align: left;">20150117</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS017412000008</td><td style = "text-align: left;">ほしの 菜摘</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1971-12-18</td><td style = "text-align: right;">47</td><td style = "text-align: left;">166-0004</td><td style = "text-align: left;">東京都杉並区阿佐谷南**********</td><td style = "text-align: left;">S13017</td><td style = "text-align: left;">20150907</td><td style = "text-align: left;">3-20080225-3</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS022403000040</td><td style = "text-align: left;">佐川 憲史</td><td style = "text-align: left;">0</td><td style = "text-align: left;">男性</td><td style = "text-align: left;">1969-09-25</td><td style = "text-align: right;">49</td><td style = "text-align: left;">240-0113</td><td style = "text-align: left;">神奈川県三浦郡葉山町長柄**********</td><td style = "text-align: left;">S14022</td><td style = "text-align: left;">20150804</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001713000040</td><td style = "text-align: left;">村山 遥</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1939-12-10</td><td style = "text-align: right;">79</td><td style = "text-align: left;">144-0045</td><td style = "text-align: left;">東京都大田区南六郷**********</td><td style = "text-align: left;">S13001</td><td style = "text-align: left;">20150323</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS023602000043</td><td style = "text-align: left;">戸田 和之</td><td style = "text-align: left;">0</td><td style = "text-align: left;">男性</td><td style = "text-align: left;">1949-01-31</td><td style = "text-align: right;">70</td><td style = "text-align: left;">212-0054</td><td style = "text-align: left;">神奈川県川崎市幸区小倉**********</td><td style = "text-align: left;">S14023</td><td style = "text-align: left;">20170114</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS003615000329</td><td style = "text-align: left;">おかやま 貴美子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1956-10-15</td><td style = "text-align: right;">62</td><td style = "text-align: left;">201-0001</td><td style = "text-align: left;">東京都狛江市西野川**********</td><td style = "text-align: left;">S13003</td><td style = "text-align: left;">20170405</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS038615000053</td><td style = "text-align: left;">門脇 優</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1951-01-03</td><td style = "text-align: right;">68</td><td style = "text-align: left;">279-0041</td><td style = "text-align: left;">千葉県浦安市堀江**********</td><td style = "text-align: left;">S13038</td><td style = "text-align: left;">20150522</td><td style = "text-align: left;">4-20090601-5</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS039413000205</td><td style = "text-align: left;">野田 季衣</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1975-02-18</td><td style = "text-align: right;">44</td><td style = "text-align: left;">167-0032</td><td style = "text-align: left;">東京都杉並区天沼**********</td><td style = "text-align: left;">S13039</td><td style = "text-align: left;">20150412</td><td style = "text-align: left;">1-20090216-4</td></tr></tbody></table></div>

**以下の方法は，各行に [0,1) の一様乱数を与え，その値が希望する抽出率の数値未満の行を抽出するというものである。実際に抽出される件数が若干の幅を持つが，実用上の問題はないであろう。**

```julia
# 別解
using StatsBase
df = df_customer |>
    @mutate(uniformrandom=rand(1)[1]) |>
    @filter(_.uniformrandom < 0.01) |>
    @select(-:uniformrandom) |>  # 乱数列は用済みなので削除
    DataFrame
println("df_customer の件数 = $(nrow(df_customer)), 抽出された件数 = $(nrow(df)), 抽出率 = $(100nrow(df)/nrow(df_customer)) %")
first(df, 10)
```

    df_customer の件数 = 21971, 抽出された件数 = 216, 抽出率 = 0.9831141049565336 %

<div><div style = "float: left;"><span>10×11 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">customer_name</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">gender</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">postal_cd</th><th style = "text-align: left;">address</th><th style = "text-align: left;">application_store_cd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">status_cd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS011215000048</td><td style = "text-align: left;">芦田 沙耶</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1992-02-01</td><td style = "text-align: right;">27</td><td style = "text-align: left;">223-0062</td><td style = "text-align: left;">神奈川県横浜市港北区日吉本町**********</td><td style = "text-align: left;">S14011</td><td style = "text-align: left;">20150228</td><td style = "text-align: left;">C-20100421-9</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS031415000106</td><td style = "text-align: left;">宇野 由美子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1970-02-26</td><td style = "text-align: right;">49</td><td style = "text-align: left;">151-0053</td><td style = "text-align: left;">東京都渋谷区代々木**********</td><td style = "text-align: left;">S13031</td><td style = "text-align: left;">20150201</td><td style = "text-align: left;">F-20100511-E</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS029313000221</td><td style = "text-align: left;">北条 ひかり</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1987-06-19</td><td style = "text-align: right;">31</td><td style = "text-align: left;">279-0011</td><td style = "text-align: left;">千葉県浦安市美浜**********</td><td style = "text-align: left;">S12029</td><td style = "text-align: left;">20180810</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS013613000043</td><td style = "text-align: left;">大森 瞳</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1956-04-28</td><td style = "text-align: right;">62</td><td style = "text-align: left;">261-0025</td><td style = "text-align: left;">千葉県千葉市美浜区浜田**********</td><td style = "text-align: left;">S12013</td><td style = "text-align: left;">20150222</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS006803000002</td><td style = "text-align: left;">堀 獅童</td><td style = "text-align: left;">0</td><td style = "text-align: left;">男性</td><td style = "text-align: left;">1930-01-21</td><td style = "text-align: right;">89</td><td style = "text-align: left;">224-0057</td><td style = "text-align: left;">神奈川県横浜市都筑区川和町**********</td><td style = "text-align: left;">S14006</td><td style = "text-align: left;">20150927</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS031514000047</td><td style = "text-align: left;">原口 礼子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1963-07-26</td><td style = "text-align: right;">55</td><td style = "text-align: left;">151-0064</td><td style = "text-align: left;">東京都渋谷区上原**********</td><td style = "text-align: left;">S13031</td><td style = "text-align: left;">20150927</td><td style = "text-align: left;">8-20090607-6</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS025713000029</td><td style = "text-align: left;">大塚 怜奈</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1942-07-31</td><td style = "text-align: right;">76</td><td style = "text-align: left;">242-0015</td><td style = "text-align: left;">神奈川県大和市下和田**********</td><td style = "text-align: left;">S14025</td><td style = "text-align: left;">20151005</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS037704000007</td><td style = "text-align: left;">吹越 菊生</td><td style = "text-align: left;">0</td><td style = "text-align: left;">男性</td><td style = "text-align: left;">1943-01-11</td><td style = "text-align: right;">76</td><td style = "text-align: left;">136-0076</td><td style = "text-align: left;">東京都江東区南砂**********</td><td style = "text-align: left;">S13037</td><td style = "text-align: left;">20150329</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS002215000165</td><td style = "text-align: left;">田村 美幸</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1991-01-16</td><td style = "text-align: right;">28</td><td style = "text-align: left;">185-0014</td><td style = "text-align: left;">東京都国分寺市東恋ケ窪**********</td><td style = "text-align: left;">S13002</td><td style = "text-align: left;">20170728</td><td style = "text-align: left;">5-20100122-1</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS007415000047</td><td style = "text-align: left;">土屋 瞬</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1973-06-25</td><td style = "text-align: right;">45</td><td style = "text-align: left;">285-0855</td><td style = "text-align: left;">千葉県佐倉市井野**********</td><td style = "text-align: left;">S12007</td><td style = "text-align: left;">20141122</td><td style = "text-align: left;">0-00000000-0</td></tr></tbody></table></div>

# 層化抽出

---
> J-076: 顧客データ（df_customer）から性別コード（gender_cd）の割合に基づきランダムに10%のデータを層化抽出し、性別コードごとに件数を集計せよ。

```julia
combine(groupby(df_customer, :gender_cd), nrow => :customer_num)
```

<div><div style = "float: left;"><span>3×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">customer_num</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String" style = "text-align: left;">String</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">1</td><td style = "text-align: right;">17918</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">9</td><td style = "text-align: right;">1072</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">0</td><td style = "text-align: right;">2981</td></tr></tbody></table></div>

```julia
function sampling(df)
    n = nrow(df)
    return df[sample(1:n, floor(Int, n*0.01), replace=false), :]
end

gd = groupby(df_customer, :gender_cd)
df = sampling(gd[1])
for i in 2:3
    df = vcat(df, sampling(gd[i]))
end

gd = groupby(df, :gender_cd)
combine(gd, nrow)
```

<div><div style = "float: left;"><span>3×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">nrow</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String" style = "text-align: left;">String</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">1</td><td style = "text-align: right;">179</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">9</td><td style = "text-align: right;">10</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">0</td><td style = "text-align: right;">29</td></tr></tbody></table></div>

**データフレームをグループ化し，それぞれのデータフレームに対して一定の処理を行い，再度一つのデータフレームにまとめるという作業は以下のようにすれば簡単になる。**

```julia
# 別解
gd = groupby(df_customer, :gender_cd)  # グループ化
df = [sampling(df) for df in gd]  # 処理（関数内で行う）
df2 = vcat(df...)  # 再度一つのデータフレームにまとめる
first(df2, 10)
```

<div><div style = "float: left;"><span>10×11 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">customer_name</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">gender</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">postal_cd</th><th style = "text-align: left;">address</th><th style = "text-align: left;">application_store_cd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">status_cd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS019415000255</td><td style = "text-align: left;">寺西 綾</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1975-09-24</td><td style = "text-align: right;">43</td><td style = "text-align: left;">173-0036</td><td style = "text-align: left;">東京都板橋区向原**********</td><td style = "text-align: left;">S13019</td><td style = "text-align: left;">20150515</td><td style = "text-align: left;">D-20100409-E</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS022412000091</td><td style = "text-align: left;">溝口 早紀</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1972-03-29</td><td style = "text-align: right;">47</td><td style = "text-align: left;">240-0112</td><td style = "text-align: left;">神奈川県三浦郡葉山町堀内**********</td><td style = "text-align: left;">S14022</td><td style = "text-align: left;">20150531</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS033513000001</td><td style = "text-align: left;">斉藤 美帆</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1964-06-18</td><td style = "text-align: right;">54</td><td style = "text-align: left;">246-0037</td><td style = "text-align: left;">神奈川県横浜市瀬谷区橋戸**********</td><td style = "text-align: left;">S14033</td><td style = "text-align: left;">20150918</td><td style = "text-align: left;">9-20090809-6</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS004515000228</td><td style = "text-align: left;">吉村 南朋</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1961-05-15</td><td style = "text-align: right;">57</td><td style = "text-align: left;">167-0022</td><td style = "text-align: left;">東京都杉並区下井草**********</td><td style = "text-align: left;">S13004</td><td style = "text-align: left;">20150214</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS035212000033</td><td style = "text-align: left;">上村 あさみ</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1993-09-25</td><td style = "text-align: right;">25</td><td style = "text-align: left;">157-0068</td><td style = "text-align: left;">東京都世田谷区宇奈根**********</td><td style = "text-align: left;">S13035</td><td style = "text-align: left;">20150415</td><td style = "text-align: left;">5-20100905-6</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001312000231</td><td style = "text-align: left;">西脇 京子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1985-10-18</td><td style = "text-align: right;">33</td><td style = "text-align: left;">210-0021</td><td style = "text-align: left;">神奈川県川崎市川崎区元木**********</td><td style = "text-align: left;">S13001</td><td style = "text-align: left;">20160324</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS022715000051</td><td style = "text-align: left;">黒谷 景子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1941-07-16</td><td style = "text-align: right;">77</td><td style = "text-align: left;">249-0005</td><td style = "text-align: left;">神奈川県逗子市桜山**********</td><td style = "text-align: left;">S14022</td><td style = "text-align: left;">20150608</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS024312000022</td><td style = "text-align: left;">三輪 瞳</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1981-02-13</td><td style = "text-align: right;">38</td><td style = "text-align: left;">216-0015</td><td style = "text-align: left;">神奈川県川崎市宮前区菅生**********</td><td style = "text-align: left;">S14024</td><td style = "text-align: left;">20150924</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001211000024</td><td style = "text-align: left;">島津 一恵</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1992-02-06</td><td style = "text-align: right;">27</td><td style = "text-align: left;">212-0058</td><td style = "text-align: left;">神奈川県川崎市幸区鹿島田**********</td><td style = "text-align: left;">S13001</td><td style = "text-align: left;">20160421</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS027315000113</td><td style = "text-align: left;">武藤 美佐</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1987-05-12</td><td style = "text-align: right;">31</td><td style = "text-align: left;">251-0016</td><td style = "text-align: left;">神奈川県藤沢市弥勒寺**********</td><td style = "text-align: left;">S14027</td><td style = "text-align: left;">20150626</td><td style = "text-align: left;">0-00000000-0</td></tr></tbody></table></div>

# 外れ値

---
> J-077: レシート明細データ（df_receipt）の売上金額を顧客単位に合計し、合計した売上金額の外れ値を抽出せよ。なお、外れ値は売上金額合計を対数化したうえで平均と標準偏差を計算し、その平均から3σを超えて離れたものとする（自然対数と常用対数のどちらでも可）。結果は10件表示せよ。

```julia
gd = groupby(df_receipt, :customer_id)
df = combine(gd, :amount => sum => :sum_amount, :amount => (x -> log(sum(x))) => :log_sum_amount);

using StatsBase
m = mean(df.log_sum_amount)
s = std(df.log_sum_amount)
df2 = df[abs.(df.log_sum_amount .- m) ./ s .> 3, :]
first(df2, 10)
```

<div><div style = "float: left;"><span>1×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sum_amount</th><th style = "text-align: left;">log_sum_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: right;">12395003</td><td style = "text-align: right;">16.3328</td></tr></tbody></table></div>

```julia
# 別解
df = df_receipt |>
    @groupby(_.customer_id) |>
    @map({customer_id=key(_), sum_amount=sum(_.amount), log_sum_amount=log(sum(_.amount))}) |>
    DataFrame

using StatsBase
m = mean(df.log_sum_amount)
s = std(df.log_sum_amount)
df2 = df |>
    @filter(abs(_.log_sum_amount - m) / s > 3) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>1×3 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sum_amount</th><th style = "text-align: left;">log_sum_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">ZZ000000000000</td><td style = "text-align: right;">12395003</td><td style = "text-align: right;">16.3328</td></tr></tbody></table></div>

---
> J-078: レシート明細データ（df_receipt）の売上金額（amount）を顧客単位に合計し、合計した売上金額の外れ値を抽出せよ。ただし、顧客IDが"Z"から始まるのものは非会員を表すため、除外して計算すること。なお、ここでは外れ値を第1四分位と第3四分位の差であるIQRを用いて、「第1四分位数-1.5×IQR」を下回るもの、または「第3四分位数+1.5×IQR」を超えるものとする。結果は10件表示せよ。

```julia
gd = groupby(filter(:customer_id => x -> !occursin(r"^Z", x), df_receipt), :customer_id)
df = combine(gd, :amount => sum => :sum_amount)
using StatsBase
(qt1, qt3) = quantile(df.sum_amount, [0.25, 0.75])
IQR = qt3 - qt1
df2 = df[df.sum_amount .< qt1 - 1.5*IQR .|| df.sum_amount .> qt3 + 1.5*IQR, :]
sort!(df2, :customer_id)
first(df2, 10)
```

<div><div style = "float: left;"><span>10×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sum_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001414000048</td><td style = "text-align: right;">8584</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001605000009</td><td style = "text-align: right;">18925</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS002415000594</td><td style = "text-align: right;">9568</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS004414000181</td><td style = "text-align: right;">9584</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS005415000137</td><td style = "text-align: right;">8734</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS006414000001</td><td style = "text-align: right;">9156</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS006414000029</td><td style = "text-align: right;">9179</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS006415000105</td><td style = "text-align: right;">10042</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS006415000147</td><td style = "text-align: right;">12723</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS006415000157</td><td style = "text-align: right;">10648</td></tr></tbody></table></div>

```julia
# 別解
df = df_receipt[!, [:customer_id, :amount]] |>
    @filter(!occursin(r"^Z", _.customer_id)) |>
    @groupby(_.customer_id) |>
    @map({customer_id=key(_), sum_amount=sum(_.amount)}) |>
    DataFrame

using StatsBase
(qt1, qt3) = quantile(df2.sum_amount, [0.25, 0.75])
IQR = qt3 - qt1

df |>
    @filter(_.sum_amount < qt1 - 1.5*IQR || _.sum_amount > qt3 + 1.5*IQR) |>
    @orderby(_.customer_id) |>
    @take(10) |>
    DataFrame
```

<div><div style = "float: left;"><span>10×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">sum_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: right;">1298</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">626</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">3044</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: right;">1988</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">3337</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001211000025</td><td style = "text-align: right;">456</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001212000027</td><td style = "text-align: right;">448</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001212000031</td><td style = "text-align: right;">296</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001212000046</td><td style = "text-align: right;">228</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001212000070</td><td style = "text-align: right;">456</td></tr></tbody></table></div>

# 欠損値の補完

---
> J-079: 商品データ（df_product）の各項目に対し、欠損数を確認せよ。

```julia
a = map(eachcol(df_product)) do col
    length(col) - length(collect(skipmissing(col)))
end;

DataFrame(variable = names(df_product), missings = a)
```

<div><div style = "float: left;"><span>6×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">variable</th><th style = "text-align: left;">missings</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String" style = "text-align: left;">String</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">product_cd</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">category_major_cd</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">category_medium_cd</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">category_small_cd</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">unit_price</td><td style = "text-align: right;">7</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">unit_cost</td><td style = "text-align: right;">7</td></tr></tbody></table></div>

---
> J-080: 商品データ（df_product）のいずれかの項目に欠損が発生しているレコードを全て削除した新たな商品データを作成せよ。なお、削除前後の件数を表示させ、079で確認した件数だけ減少していることも確認すること。

```julia
df = df_product |> dropmissing
println("欠損値削除前の件数 = $(nrow(df_product)),  欠損値削除後の件数 = $(nrow(df))")
```

    欠損値削除前の件数 = 10030,  欠損値削除後の件数 = 10023

---
> J-081: 単価（unit_price）と原価（unit_cost）の欠損値について、それぞれの平均値で補完した新たな商品データを作成せよ。なお、平均値については1円未満を丸めること（四捨五入または偶数への丸めで良い）。補完実施後、各項目について欠損が生じていないことも確認すること。

```julia
price_mean = round(Int, mean(skipmissing(df_product.unit_price)))
cost_mean  = round(Int, mean(skipmissing(df_product.unit_cost)))
println("price_mean = $price_mean,  cost_mean = $cost_mean")
df = copy(df_product)
println(df[[159, 160, 197, 497, 1532, 2013, 6297, 7076], :])
println("nrow = $(nrow(df))")
df.unit_price = coalesce.(df.unit_price, price_mean)
df.unit_cost  = coalesce.(df.unit_cost,  cost_mean)
println(df[[159, 160, 197, 497, 1532, 2013, 6297, 7076], :])
println("nrow = $(nrow(df))")
```

    price_mean = 403,  cost_mean = 302
    [1m8×6 DataFrame[0m
    [1m Row [0m│[1m product_cd [0m[1m category_major_cd [0m[1m category_medium_cd [0m[1m category_small_cd [0m[1m unit_price [0m[1m unit_cost [0m
         │[90m String15   [0m[90m String            [0m[90m String             [0m[90m String            [0m[90m Int64?     [0m[90m Int64?    [0m
    ─────┼─────────────────────────────────────────────────────────────────────────────────────────────
       1 │ P040802006  04                 0408                040802                    438        329
       2 │ P040802007  04                 0408                040802            [90m    missing [0m[90m   missing [0m
       3 │ P050103021  05                 0501                050103            [90m    missing [0m[90m   missing [0m
       4 │ P050405009  05                 0504                050405            [90m    missing [0m[90m   missing [0m
       5 │ P060802026  06                 0608                060802            [90m    missing [0m[90m   missing [0m
       6 │ P070202092  07                 0702                070202            [90m    missing [0m[90m   missing [0m
       7 │ P080504027  08                 0805                080504            [90m    missing [0m[90m   missing [0m
       8 │ P090204185  09                 0902                090204            [90m    missing [0m[90m   missing [0m
    nrow = 10030
    [1m8×6 DataFrame[0m
    [1m Row [0m│[1m product_cd [0m[1m category_major_cd [0m[1m category_medium_cd [0m[1m category_small_cd [0m[1m unit_price [0m[1m unit_cost [0m
         │[90m String15   [0m[90m String            [0m[90m String             [0m[90m String            [0m[90m Int64      [0m[90m Int64     [0m
    ─────┼─────────────────────────────────────────────────────────────────────────────────────────────
       1 │ P040802006  04                 0408                040802                    438        329
       2 │ P040802007  04                 0408                040802                    403        302
       3 │ P050103021  05                 0501                050103                    403        302
       4 │ P050405009  05                 0504                050405                    403        302
       5 │ P060802026  06                 0608                060802                    403        302
       6 │ P070202092  07                 0702                070202                    403        302
       7 │ P080504027  08                 0805                080504                    403        302
       8 │ P090204185  09                 0902                090204                    403        302
    nrow = 10030

```julia
# 別解
using StatsBase
price_mean = round(Int, mean(skipmissing(df_product.unit_price)))
cost_mean  = round(Int, mean(skipmissing(df_product.unit_cost)))
println(df_product[[159, 160, 197, 497, 1532, 2013, 6297, 7076], :])
println("nrow = $(nrow(df))")
df = df_product |>
    # @replacena(:unit_price => price_mean, :unit_cost => cost_mean) |> エラーになる。変数名は指定できない
    @replacena(:unit_price => 9999999999, :unit_cost => 9999999999) |>
    @mutate(unit_price = _.unit_price == 9999999999 ? price_mean : _.unit_price,
            unit_cost  = _.unit_cost  == 9999999999 ? cost_mean  : _.unit_cost) |>
    DataFrame
println("nrow = $(nrow(df))")
println(df[[159, 160, 197, 497, 1532, 2013, 6297, 7076], :])

```

    [1m8×6 DataFrame[0m
    [1m Row [0m│[1m product_cd [0m[1m category_major_cd [0m[1m category_medium_cd [0m[1m category_small_cd [0m[1m unit_price [0m[1m unit_cost [0m
         │[90m String15   [0m[90m String            [0m[90m String             [0m[90m String            [0m[90m Int64?     [0m[90m Int64?    [0m
    ─────┼─────────────────────────────────────────────────────────────────────────────────────────────
       1 │ P040802006  04                 0408                040802                    438        329
       2 │ P040802007  04                 0408                040802            [90m    missing [0m[90m   missing [0m
       3 │ P050103021  05                 0501                050103            [90m    missing [0m[90m   missing [0m
       4 │ P050405009  05                 0504                050405            [90m    missing [0m[90m   missing [0m
       5 │ P060802026  06                 0608                060802            [90m    missing [0m[90m   missing [0m
       6 │ P070202092  07                 0702                070202            [90m    missing [0m[90m   missing [0m
       7 │ P080504027  08                 0805                080504            [90m    missing [0m[90m   missing [0m
       8 │ P090204185  09                 0902                090204            [90m    missing [0m[90m   missing [0m
    nrow = 10030
    nrow = 10030
    [1m8×6 DataFrame[0m
    [1m Row [0m│[1m product_cd [0m[1m category_major_cd [0m[1m category_medium_cd [0m[1m category_small_cd [0m[1m unit_price [0m[1m unit_cost [0m
         │[90m Any        [0m[90m Any               [0m[90m Any                [0m[90m Any               [0m[90m Any        [0m[90m Any       [0m
    ─────┼─────────────────────────────────────────────────────────────────────────────────────────────
       1 │ P040802006  04                 0408                040802             438         329
       2 │ P040802007  04                 0408                040802             403         302
       3 │ P050103021  05                 0501                050103             403         302
       4 │ P050405009  05                 0504                050405             403         302
       5 │ P060802026  06                 0608                060802             403         302
       6 │ P070202092  07                 0702                070202             403         302
       7 │ P080504027  08                 0805                080504             403         302
       8 │ P090204185  09                 0902                090204             403         302

---
> J-082: 単価（unit_price）と原価（unit_cost）の欠損値について、それぞれの中央値で補完した新たな商品データを作成せよ。なお、中央値については1円未満を丸めること（四捨五入または偶数への丸めで良い）。補完実施後、各項目について欠損が生じていないことも確認すること。

```julia
price_median = round(Int, median(skipmissing(df_product.unit_price)))
cost_median  = round(Int, median(skipmissing(df_product.unit_cost)))
println("price_median = $price_median,  cost_median = $cost_median")
df = copy(df_product)
println(df[[159, 160, 197, 497, 1532, 2013, 6297, 7076], :])
println("nrow = $(nrow(df))")
df.unit_price = coalesce.(df.unit_price, price_median)
df.unit_cost  = coalesce.(df.unit_cost,  cost_median)
println(df[[159, 160, 197, 497, 1532, 2013, 6297, 7076], :])
println("nrow = $(nrow(df))")
```

    price_median = 252,  cost_median = 189
    [1m8×6 DataFrame[0m
    [1m Row [0m│[1m product_cd [0m[1m category_major_cd [0m[1m category_medium_cd [0m[1m category_small_cd [0m[1m unit_price [0m[1m unit_cost [0m
         │[90m String15   [0m[90m String            [0m[90m String             [0m[90m String            [0m[90m Int64?     [0m[90m Int64?    [0m
    ─────┼─────────────────────────────────────────────────────────────────────────────────────────────
       1 │ P040802006  04                 0408                040802                    438        329
       2 │ P040802007  04                 0408                040802            [90m    missing [0m[90m   missing [0m
       3 │ P050103021  05                 0501                050103            [90m    missing [0m[90m   missing [0m
       4 │ P050405009  05                 0504                050405            [90m    missing [0m[90m   missing [0m
       5 │ P060802026  06                 0608                060802            [90m    missing [0m[90m   missing [0m
       6 │ P070202092  07                 0702                070202            [90m    missing [0m[90m   missing [0m
       7 │ P080504027  08                 0805                080504            [90m    missing [0m[90m   missing [0m
       8 │ P090204185  09                 0902                090204            [90m    missing [0m[90m   missing [0m
    nrow = 10030
    [1m8×6 DataFrame[0m
    [1m Row [0m│[1m product_cd [0m[1m category_major_cd [0m[1m category_medium_cd [0m[1m category_small_cd [0m[1m unit_price [0m[1m unit_cost [0m
         │[90m String15   [0m[90m String            [0m[90m String             [0m[90m String            [0m[90m Int64      [0m[90m Int64     [0m
    ─────┼─────────────────────────────────────────────────────────────────────────────────────────────
       1 │ P040802006  04                 0408                040802                    438        329
       2 │ P040802007  04                 0408                040802                    252        189
       3 │ P050103021  05                 0501                050103                    252        189
       4 │ P050405009  05                 0504                050405                    252        189
       5 │ P060802026  06                 0608                060802                    252        189
       6 │ P070202092  07                 0702                070202                    252        189
       7 │ P080504027  08                 0805                080504                    252        189
       8 │ P090204185  09                 0902                090204                    252        189
    nrow = 10030

```julia
# 別解
price_median = round(Int, median(skipmissing(df_product.unit_price)))
cost_median  = round(Int, median(skipmissing(df_product.unit_cost)))
println("price_median = $price_median,  cost_median = $cost_median")
println("nrow = $(nrow(df))")
println(df_product[[159, 160, 197, 497, 1532, 2013, 6297, 7076], :])
df = df_product |>
    # @replacena(:unit_price => price_median, :unit_cost => cost_median) |> エラーになる。変数名は指定できない
    @replacena(:unit_price => 9999999999, :unit_cost => 9999999999) |>
    @mutate(unit_price = _.unit_price == 9999999999 ? price_median : _.unit_price,
            unit_cost  = _.unit_cost  == 9999999999 ? cost_median  : _.unit_cost) |>
    DataFrame
println("nrow = $(nrow(df))")
println(df[[159, 160, 197, 497, 1532, 2013, 6297, 7076], :])

```

    price_median = 252,  cost_median = 189
    nrow = 10030
    [1m8×6 DataFrame[0m
    [1m Row [0m│[1m product_cd [0m[1m category_major_cd [0m[1m category_medium_cd [0m[1m category_small_cd [0m[1m unit_price [0m[1m unit_cost [0m
         │[90m String15   [0m[90m String            [0m[90m String             [0m[90m String            [0m[90m Int64?     [0m[90m Int64?    [0m
    ─────┼─────────────────────────────────────────────────────────────────────────────────────────────
       1 │ P040802006  04                 0408                040802                    438        329
       2 │ P040802007  04                 0408                040802            [90m    missing [0m[90m   missing [0m
       3 │ P050103021  05                 0501                050103            [90m    missing [0m[90m   missing [0m
       4 │ P050405009  05                 0504                050405            [90m    missing [0m[90m   missing [0m
       5 │ P060802026  06                 0608                060802            [90m    missing [0m[90m   missing [0m
       6 │ P070202092  07                 0702                070202            [90m    missing [0m[90m   missing [0m
       7 │ P080504027  08                 0805                080504            [90m    missing [0m[90m   missing [0m
       8 │ P090204185  09                 0902                090204            [90m    missing [0m[90m   missing [0m
    nrow = 10030
    [1m8×6 DataFrame[0m
    [1m Row [0m│[1m product_cd [0m[1m category_major_cd [0m[1m category_medium_cd [0m[1m category_small_cd [0m[1m unit_price [0m[1m unit_cost [0m
         │[90m Any        [0m[90m Any               [0m[90m Any                [0m[90m Any               [0m[90m Any        [0m[90m Any       [0m
    ─────┼─────────────────────────────────────────────────────────────────────────────────────────────
       1 │ P040802006  04                 0408                040802             438         329
       2 │ P040802007  04                 0408                040802             252         189
       3 │ P050103021  05                 0501                050103             252         189
       4 │ P050405009  05                 0504                050405             252         189
       5 │ P060802026  06                 0608                060802             252         189
       6 │ P070202092  07                 0702                070202             252         189
       7 │ P080504027  08                 0805                080504             252         189
       8 │ P090204185  09                 0902                090204             252         189

---
> J-083: 単価（unit_price）と原価（unit_cost）の欠損値について、各商品のカテゴリ小区分コード（category_small_cd）ごとに算出した中央値で補完した新たな商品データを作成せよ。なお、中央値については1円未満を丸めること（四捨五入または偶数への丸めで良い）。補完実施後、各項目について欠損が生じていないことも確認すること。

```julia
using StatsBase

gd = groupby(df_product, :category_small_cd)
df = combine(gd, :unit_price => (x -> round(Int, median(skipmissing(x)))) => :unit_price_median,
                 :unit_cost  => (x -> round(Int, median(skipmissing(x)))) => :unit_cost_median);
```

```julia
df2 = innerjoin(df_product, df, on=:category_small_cd);
```

```julia
# これは動かない
# results = df2 |>
#     @mutate(unit_price2 = ifelse(ismissing(_.unit_price), _.unit_price_median, _.unit_price)) |>
#     DataFrame;
```

```julia
println("Before")
println(df2[[159, 160, 197, 497, 1532, 2013, 6297, 7076], :])
for i in 1:nrow(df2)
    ismissing(df2.unit_price[i]) && (df2.unit_price[i] = df2.unit_price_median[i])
    ismissing(df2.unit_cost[i]) && (df2.unit_cost[i] = df2.unit_cost_median[i])
end
println("After")
println(df2[[159, 160, 197, 497, 1532, 2013, 6297, 7076], :])
```

    Before
    [1m8×8 DataFrame[0m
    [1m Row [0m│[1m product_cd [0m[1m category_major_cd [0m[1m category_medium_cd [0m[1m category_small_cd [0m[1m unit_price [0m[1m unit_cost [0m[1m unit_price_median [0m[1m unit_cost_median [0m
         │[90m String15   [0m[90m String            [0m[90m String             [0m[90m String            [0m[90m Int64?     [0m[90m Int64?    [0m[90m Int64             [0m[90m Int64            [0m
    ─────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       1 │ P040802006  04                 0408                040802                    438        329                313               235
       2 │ P040802007  04                 0408                040802            [90m    missing [0m[90m   missing [0m               313               235
       3 │ P050103021  05                 0501                050103            [90m    missing [0m[90m   missing [0m               132               100
       4 │ P050405009  05                 0504                050405            [90m    missing [0m[90m   missing [0m               178               134
       5 │ P060802026  06                 0608                060802            [90m    missing [0m[90m   missing [0m               270               200
       6 │ P070202092  07                 0702                070202            [90m    missing [0m[90m   missing [0m               238               179
       7 │ P080504027  08                 0805                080504            [90m    missing [0m[90m   missing [0m               258               196
       8 │ P090204185  09                 0902                090204            [90m    missing [0m[90m   missing [0m               694               521
    After
    [1m8×8 DataFrame[0m
    [1m Row [0m│[1m product_cd [0m[1m category_major_cd [0m[1m category_medium_cd [0m[1m category_small_cd [0m[1m unit_price [0m[1m unit_cost [0m[1m unit_price_median [0m[1m unit_cost_median [0m
         │[90m String15   [0m[90m String            [0m[90m String             [0m[90m String            [0m[90m Int64?     [0m[90m Int64?    [0m[90m Int64             [0m[90m Int64            [0m
    ─────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       1 │ P040802006  04                 0408                040802                    438        329                313               235
       2 │ P040802007  04                 0408                040802                    313        235                313               235
       3 │ P050103021  05                 0501                050103                    132        100                132               100
       4 │ P050405009  05                 0504                050405                    178        134                178               134
       5 │ P060802026  06                 0608                060802                    270        200                270               200
       6 │ P070202092  07                 0702                070202                    238        179                238               179
       7 │ P080504027  08                 0805                080504                    258        196                258               196
       8 │ P090204185  09                 0902                090204                    694        521                694               521

---
> J-084: 顧客データ（df_customer）の全顧客に対して全期間の売上金額に占める2019年売上金額の割合を計算し、新たなデータを作成せよ。ただし、売上実績がない場合は0として扱うこと。そして計算した割合が0超のものを抽出し、結果を10件表示せよ。また、作成したデータに欠損が存在しないことを確認せよ。

```julia
gd2019 = groupby(df_receipt[df_receipt.sales_ymd .÷ 10000 .== 2019, [:customer_id, :amount]], :customer_id)
df2019 = combine(gd2019, :amount => sum => :amount_2019);
```

```julia
gdall = groupby(df_receipt[!, [:customer_id, :amount]], :customer_id)
dfall = combine(gdall, :amount => sum => :amount_all);
```

```julia
df = leftjoin(df2019, dfall, on=:customer_id) |>
    @replacena(0) |>
    @mutate(amount_rate = _.amount_2019 / _.amount_all) |>
    @filter(_.amount_rate > 0) |>
    @orderby(_.customer_id) |>
    DataFrame
first(df, 10)
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">amount_2019</th><th style = "text-align: left;">amount_all</th><th style = "text-align: left;">amount_rate</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: right;">1298</td><td style = "text-align: right;">1298</td><td style = "text-align: right;">1.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001114000005</td><td style = "text-align: right;">188</td><td style = "text-align: right;">626</td><td style = "text-align: right;">0.300319</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001115000010</td><td style = "text-align: right;">578</td><td style = "text-align: right;">3044</td><td style = "text-align: right;">0.189882</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001205000004</td><td style = "text-align: right;">702</td><td style = "text-align: right;">1988</td><td style = "text-align: right;">0.353119</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001205000006</td><td style = "text-align: right;">486</td><td style = "text-align: right;">3337</td><td style = "text-align: right;">0.14564</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001211000025</td><td style = "text-align: right;">456</td><td style = "text-align: right;">456</td><td style = "text-align: right;">1.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001212000070</td><td style = "text-align: right;">456</td><td style = "text-align: right;">456</td><td style = "text-align: right;">1.0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001214000009</td><td style = "text-align: right;">664</td><td style = "text-align: right;">4685</td><td style = "text-align: right;">0.141729</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001214000017</td><td style = "text-align: right;">2962</td><td style = "text-align: right;">4132</td><td style = "text-align: right;">0.716844</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001214000048</td><td style = "text-align: right;">1889</td><td style = "text-align: right;">2374</td><td style = "text-align: right;">0.795703</td></tr></tbody></table></div>

```julia
a = map(eachcol(df)) do col
    length(col) - length(collect(skipmissing(col)))
end;

DataFrame(variable = names(df), missings = a)
```

<div><div style = "float: left;"><span>4×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">variable</th><th style = "text-align: left;">missings</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String" style = "text-align: left;">String</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">customer_id</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">amount_2019</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">amount_all</td><td style = "text-align: right;">0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">amount_rate</td><td style = "text-align: right;">0</td></tr></tbody></table></div>

# 位置情報（経緯度と距離）

---
> J-085: 顧客データ（df_customer）の全顧客に対し、郵便番号（postal_cd）を用いてジオコードデータ（df_geocode）を紐付け、新たな顧客データを作成せよ。ただし、1つの郵便番号（postal_cd）に複数の経度（longitude）、緯度（latitude）情報が紐づく場合は、経度（longitude）、緯度（latitude）の平均値を算出して使用すること。また、作成結果を確認するために結果を10件表示せよ。

```julia
using StatsBase
gd = groupby(df_geocode[!, [:postal_cd, :longitude, :latitude]], :postal_cd)
df = combine(gd, :longitude => mean => :m_longitude, :latitude => mean => :m_latitude);

df_customer2 = innerjoin(df_customer, df, on=:postal_cd)
first(df_customer2, 10)
```

<div><div style = "float: left;"><span>10×13 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">customer_name</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">gender</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">postal_cd</th><th style = "text-align: left;">address</th><th style = "text-align: left;">application_store_cd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">status_cd</th><th style = "text-align: left;">m_longitude</th><th style = "text-align: left;">m_latitude</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Float64" style = "text-align: left;">Float64</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS020301000012</td><td style = "text-align: left;">都築 育二</td><td style = "text-align: left;">0</td><td style = "text-align: left;">男性</td><td style = "text-align: left;">1985-08-20</td><td style = "text-align: right;">33</td><td style = "text-align: left;">332-0031</td><td style = "text-align: left;">埼玉県川口市青木**********</td><td style = "text-align: left;">S13020</td><td style = "text-align: left;">20161018</td><td style = "text-align: left;">0-00000000-0</td><td style = "text-align: right;">139.727</td><td style = "text-align: right;">35.8088</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS051412000011</td><td style = "text-align: left;">奥村 愛</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1969-06-04</td><td style = "text-align: right;">49</td><td style = "text-align: left;">332-0023</td><td style = "text-align: left;">埼玉県川口市飯塚**********</td><td style = "text-align: left;">S13051</td><td style = "text-align: left;">20180702</td><td style = "text-align: left;">0-00000000-0</td><td style = "text-align: right;">139.711</td><td style = "text-align: right;">35.7965</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS051412000012</td><td style = "text-align: left;">おかやま 未華子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1977-03-13</td><td style = "text-align: right;">42</td><td style = "text-align: left;">332-0023</td><td style = "text-align: left;">埼玉県川口市飯塚**********</td><td style = "text-align: left;">S13051</td><td style = "text-align: left;">20180511</td><td style = "text-align: left;">0-00000000-0</td><td style = "text-align: right;">139.711</td><td style = "text-align: right;">35.7965</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS051212000001</td><td style = "text-align: left;">美木 瞬</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1991-10-19</td><td style = "text-align: right;">27</td><td style = "text-align: left;">332-0023</td><td style = "text-align: left;">埼玉県川口市飯塚**********</td><td style = "text-align: left;">S13051</td><td style = "text-align: left;">20180509</td><td style = "text-align: left;">2-20101018-4</td><td style = "text-align: right;">139.711</td><td style = "text-align: right;">35.7965</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS020112000003</td><td style = "text-align: left;">荒川 まなみ</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">2004-05-13</td><td style = "text-align: right;">14</td><td style = "text-align: left;">332-0015</td><td style = "text-align: left;">埼玉県川口市川口**********</td><td style = "text-align: left;">S13020</td><td style = "text-align: left;">20151116</td><td style = "text-align: left;">0-00000000-0</td><td style = "text-align: right;">139.716</td><td style = "text-align: right;">35.8023</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS020212000016</td><td style = "text-align: left;">藤沢 恵梨香</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1991-08-08</td><td style = "text-align: right;">27</td><td style = "text-align: left;">332-0015</td><td style = "text-align: left;">埼玉県川口市川口**********</td><td style = "text-align: left;">S13020</td><td style = "text-align: left;">20150122</td><td style = "text-align: left;">4-20100207-3</td><td style = "text-align: right;">139.716</td><td style = "text-align: right;">35.8023</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS051512000001</td><td style = "text-align: left;">田原 夏希</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1959-11-21</td><td style = "text-align: right;">59</td><td style = "text-align: left;">332-0015</td><td style = "text-align: left;">埼玉県川口市川口**********</td><td style = "text-align: left;">S13051</td><td style = "text-align: left;">20190325</td><td style = "text-align: left;">0-00000000-0</td><td style = "text-align: right;">139.716</td><td style = "text-align: right;">35.8023</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS020212000004</td><td style = "text-align: left;">前田 美佐</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1989-12-11</td><td style = "text-align: right;">29</td><td style = "text-align: left;">332-0015</td><td style = "text-align: left;">埼玉県川口市川口**********</td><td style = "text-align: left;">S13020</td><td style = "text-align: left;">20150814</td><td style = "text-align: left;">C-20090906-9</td><td style = "text-align: right;">139.716</td><td style = "text-align: right;">35.8023</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS051502000001</td><td style = "text-align: left;">若山 哲平</td><td style = "text-align: left;">0</td><td style = "text-align: left;">男性</td><td style = "text-align: left;">1961-10-07</td><td style = "text-align: right;">57</td><td style = "text-align: left;">332-0015</td><td style = "text-align: left;">埼玉県川口市川口**********</td><td style = "text-align: left;">S13051</td><td style = "text-align: left;">20180209</td><td style = "text-align: left;">0-00000000-0</td><td style = "text-align: right;">139.716</td><td style = "text-align: right;">35.8023</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS020212000008</td><td style = "text-align: left;">板垣 瞳</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1997-01-05</td><td style = "text-align: right;">22</td><td style = "text-align: left;">332-0015</td><td style = "text-align: left;">埼玉県川口市川口**********</td><td style = "text-align: left;">S13020</td><td style = "text-align: left;">20150105</td><td style = "text-align: left;">0-00000000-0</td><td style = "text-align: right;">139.716</td><td style = "text-align: right;">35.8023</td></tr></tbody></table></div>

---
> J-086: 085で作成した緯度経度つき顧客データに対し、会員申込店舗コード（application_store_cd）をキーに店舗データ（df_store）と結合せよ。そして申込み店舗の緯度（latitude）・経度情報（longitude)と顧客住所（address）の緯度・経度を用いて申込み店舗と顧客住所の距離（単位：km）を求め、顧客ID（customer_id）、顧客住所（address）、店舗住所（address）とともに表示せよ。計算式は以下の簡易式で良いものとするが、その他精度の高い方式を利用したライブラリを利用してもかまわない。結果は10件表示せよ。

$$
\mbox{緯度（ラジアン）}：\phi \\
\mbox{経度（ラジアン）}：\lambda \\
\mbox{距離}L = 6371 * \arccos(\sin \phi_1 * \sin \phi_2
+ \cos \phi_1 * \cos \phi_2 * \cos(\lambda_1 − \lambda_2))
$$

```julia
calc_distance(x1, y1, x2, y2) = 6371 .* acos.(
                        sin.(x1 * pi / 180) 
                        * sin.(x2 .* pi / 180) 
                        + cos.(x1 .* pi / 180) 
                        * cos.(x2 .* pi / 180) 
                        * cos.((y1 .* pi / 180) - (y2 .* pi / 180 )))
```

    calc_distance (generic function with 1 method)

```julia
result = innerjoin(df_customer2, df_store, on=:application_store_cd => :store_cd, makeunique=true);
```

```julia
result_df = result |>
    @mutate(distance = calc_distance(_.m_latitude, _.m_longitude, _.latitude, _.longitude)) |>
    @rename(:address => :customer_address, :address_1 => :store_address) |>
    @select(:customer_id, :customer_address, :store_address, :distance) |>
    @orderby(_.customer_id) |>
    DataFrame
first(result_df, 10)
```

<div><div style = "float: left;"><span>10×4 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">customer_address</th><th style = "text-align: left;">store_address</th><th style = "text-align: left;">distance</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String" style = "text-align: left;">String</th><th title = "Float64" style = "text-align: left;">Float64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS001105000001</td><td style = "text-align: left;">東京都大田区西六郷**********</td><td style = "text-align: left;">東京都大田区仲六郷二丁目</td><td style = "text-align: right;">1.47979</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001112000009</td><td style = "text-align: left;">東京都大田区西馬込**********</td><td style = "text-align: left;">東京都大田区仲六郷二丁目</td><td style = "text-align: right;">4.02049</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS001112000019</td><td style = "text-align: left;">東京都大田区昭和島**********</td><td style = "text-align: left;">東京都大田区仲六郷二丁目</td><td style = "text-align: right;">3.78301</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS001112000021</td><td style = "text-align: left;">東京都大田区西六郷**********</td><td style = "text-align: left;">東京都大田区仲六郷二丁目</td><td style = "text-align: right;">1.47979</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001112000023</td><td style = "text-align: left;">東京都大田区昭和島**********</td><td style = "text-align: left;">東京都大田区仲六郷二丁目</td><td style = "text-align: right;">3.78301</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS001112000024</td><td style = "text-align: left;">東京都大田区西六郷**********</td><td style = "text-align: left;">東京都大田区仲六郷二丁目</td><td style = "text-align: right;">1.47979</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS001112000029</td><td style = "text-align: left;">東京都大田区西六郷**********</td><td style = "text-align: left;">東京都大田区仲六郷二丁目</td><td style = "text-align: right;">1.47979</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS001112000030</td><td style = "text-align: left;">東京都大田区西六郷**********</td><td style = "text-align: left;">東京都大田区仲六郷二丁目</td><td style = "text-align: right;">1.47979</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS001113000004</td><td style = "text-align: left;">東京都大田区西六郷**********</td><td style = "text-align: left;">東京都大田区仲六郷二丁目</td><td style = "text-align: right;">1.47979</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS001113000010</td><td style = "text-align: left;">東京都大田区西六郷**********</td><td style = "text-align: left;">東京都大田区仲六郷二丁目</td><td style = "text-align: right;">1.47979</td></tr></tbody></table></div>

# 名寄データ

---
> J-087: 顧客データ（df_customer）では、異なる店舗での申込みなどにより同一顧客が複数登録されている。名前（customer_name）と郵便番号（postal_cd）が同じ顧客は同一顧客とみなして1顧客1レコードとなるように名寄せした名寄顧客データを作成し、顧客データの件数、名寄顧客データの件数、重複数を算出せよ。ただし、同一顧客に対しては売上金額合計が最も高いものを残し、売上金額合計が同一もしくは売上実績がない顧客については顧客ID（customer_id）の番号が小さいものを残すこととする。

```julia
gd = groupby(df_receipt, :customer_id)
df = leftjoin(df_customer, combine(gd, :amount => sum => :sum_amount), on=:customer_id);

gd = groupby(df, [:customer_name, :postal_cd]);
```

```julia
count = 0
df_customer_u = DataFrame()
for dfx in gd
    if nrow(dfx) > 1
        for i in 1:nrow(dfx)
            if ismissing(dfx.sum_amount[i])
                dfx.sum_amount[i] = 0
            end
        end
        count += 1
       dfx =  dfx |>
            @orderby_descending(_.sum_amount) |>
            @thenby(_.customer_id) |>
            @take(1) |>
            DataFrame
        #println(dfx)
    end
    append!(df_customer_u, dfx)
end
println("df_customer   の行数 = ", nrow(df_customer))
println("df_customer_u の行数 = ", nrow(df_customer_u))
println("差 = ", count)
first(df_customer_u, 5)
```

    df_customer   の行数 = 21971
    df_customer_u の行数 = 21941
    差 = 30

<div><div style = "float: left;"><span>5×12 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">customer_name</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">gender</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">postal_cd</th><th style = "text-align: left;">address</th><th style = "text-align: left;">application_store_cd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">status_cd</th><th style = "text-align: left;">sum_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Union{Missing, Int64}" style = "text-align: left;">Int64?</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS031415000172</td><td style = "text-align: left;">宇多田 貴美子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1976-10-04</td><td style = "text-align: right;">42</td><td style = "text-align: left;">151-0053</td><td style = "text-align: left;">東京都渋谷区代々木**********</td><td style = "text-align: left;">S13031</td><td style = "text-align: left;">20150529</td><td style = "text-align: left;">D-20100325-C</td><td style = "text-align: right;">5088</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS001215000145</td><td style = "text-align: left;">田崎 美紀</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1995-03-29</td><td style = "text-align: right;">24</td><td style = "text-align: left;">144-0055</td><td style = "text-align: left;">東京都大田区仲六郷**********</td><td style = "text-align: left;">S13001</td><td style = "text-align: left;">20170605</td><td style = "text-align: left;">6-20090929-2</td><td style = "text-align: right;">875</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS015414000103</td><td style = "text-align: left;">奥野 陽子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1977-08-09</td><td style = "text-align: right;">41</td><td style = "text-align: left;">136-0073</td><td style = "text-align: left;">東京都江東区北砂**********</td><td style = "text-align: left;">S13015</td><td style = "text-align: left;">20150722</td><td style = "text-align: left;">B-20100609-B</td><td style = "text-align: right;">3122</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS033513000180</td><td style = "text-align: left;">安斎 遥</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1962-07-11</td><td style = "text-align: right;">56</td><td style = "text-align: left;">241-0823</td><td style = "text-align: left;">神奈川県横浜市旭区善部町**********</td><td style = "text-align: left;">S14033</td><td style = "text-align: left;">20150728</td><td style = "text-align: left;">6-20080506-5</td><td style = "text-align: right;">868</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS011215000048</td><td style = "text-align: left;">芦田 沙耶</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1992-02-01</td><td style = "text-align: right;">27</td><td style = "text-align: left;">223-0062</td><td style = "text-align: left;">神奈川県横浜市港北区日吉本町**********</td><td style = "text-align: left;">S14011</td><td style = "text-align: left;">20150228</td><td style = "text-align: left;">C-20100421-9</td><td style = "text-align: right;">3444</td></tr></tbody></table></div>

---
> J-088: 087で作成したデータを元に、顧客データに統合名寄IDを付与したデータを作成せよ。ただし、統合名寄IDは以下の仕様で付与するものとする。
>
> - 重複していない顧客：顧客ID（customer_id）を設定
> - 重複している顧客：前設問で抽出したレコードの顧客IDを設定
> 
> 顧客IDのユニーク件数と、統合名寄IDのユニーク件数の差も確認すること。

```julia
df_customer_n = innerjoin(df_customer, df_customer_u[!, [:customer_id, :customer_name, :postal_cd]], on=[:customer_name, :postal_cd], makeunique=true) |>
    @rename(:customer_id_1 => :integration_id) |>
    DataFrame;
println("件数の差 = $(length(unique(df_customer_n.customer_id)) - length(unique(df_customer_n.integration_id)))")
first(df_customer_n, 10)

```

    件数の差 = 30

<div><div style = "float: left;"><span>10×12 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">customer_name</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">gender</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">postal_cd</th><th style = "text-align: left;">address</th><th style = "text-align: left;">application_store_cd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">status_cd</th><th style = "text-align: left;">integration_id</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String15" style = "text-align: left;">String15</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS021313000114</td><td style = "text-align: left;">大野 あや子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1981-04-29</td><td style = "text-align: right;">37</td><td style = "text-align: left;">259-1113</td><td style = "text-align: left;">神奈川県伊勢原市粟窪**********</td><td style = "text-align: left;">S14021</td><td style = "text-align: left;">20150905</td><td style = "text-align: left;">0-00000000-0</td><td style = "text-align: left;">CS021313000114</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS037613000071</td><td style = "text-align: left;">六角 雅彦</td><td style = "text-align: left;">9</td><td style = "text-align: left;">不明</td><td style = "text-align: left;">1952-04-01</td><td style = "text-align: right;">66</td><td style = "text-align: left;">136-0076</td><td style = "text-align: left;">東京都江東区南砂**********</td><td style = "text-align: left;">S13037</td><td style = "text-align: left;">20150414</td><td style = "text-align: left;">0-00000000-0</td><td style = "text-align: left;">CS037613000071</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS031415000172</td><td style = "text-align: left;">宇多田 貴美子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1976-10-04</td><td style = "text-align: right;">42</td><td style = "text-align: left;">151-0053</td><td style = "text-align: left;">東京都渋谷区代々木**********</td><td style = "text-align: left;">S13031</td><td style = "text-align: left;">20150529</td><td style = "text-align: left;">D-20100325-C</td><td style = "text-align: left;">CS031415000172</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS028811000001</td><td style = "text-align: left;">堀井 かおり</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1933-03-27</td><td style = "text-align: right;">86</td><td style = "text-align: left;">245-0016</td><td style = "text-align: left;">神奈川県横浜市泉区和泉町**********</td><td style = "text-align: left;">S14028</td><td style = "text-align: left;">20160115</td><td style = "text-align: left;">0-00000000-0</td><td style = "text-align: left;">CS028811000001</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS001215000145</td><td style = "text-align: left;">田崎 美紀</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1995-03-29</td><td style = "text-align: right;">24</td><td style = "text-align: left;">144-0055</td><td style = "text-align: left;">東京都大田区仲六郷**********</td><td style = "text-align: left;">S13001</td><td style = "text-align: left;">20170605</td><td style = "text-align: left;">6-20090929-2</td><td style = "text-align: left;">CS001215000145</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">6</td><td style = "text-align: left;">CS020401000016</td><td style = "text-align: left;">宮下 達士</td><td style = "text-align: left;">0</td><td style = "text-align: left;">男性</td><td style = "text-align: left;">1974-09-15</td><td style = "text-align: right;">44</td><td style = "text-align: left;">174-0065</td><td style = "text-align: left;">東京都板橋区若木**********</td><td style = "text-align: left;">S13020</td><td style = "text-align: left;">20150225</td><td style = "text-align: left;">0-00000000-0</td><td style = "text-align: left;">CS020401000016</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">7</td><td style = "text-align: left;">CS015414000103</td><td style = "text-align: left;">奥野 陽子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1977-08-09</td><td style = "text-align: right;">41</td><td style = "text-align: left;">136-0073</td><td style = "text-align: left;">東京都江東区北砂**********</td><td style = "text-align: left;">S13015</td><td style = "text-align: left;">20150722</td><td style = "text-align: left;">B-20100609-B</td><td style = "text-align: left;">CS015414000103</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">8</td><td style = "text-align: left;">CS029403000008</td><td style = "text-align: left;">釈 人志</td><td style = "text-align: left;">0</td><td style = "text-align: left;">男性</td><td style = "text-align: left;">1973-08-17</td><td style = "text-align: right;">45</td><td style = "text-align: left;">279-0003</td><td style = "text-align: left;">千葉県浦安市海楽**********</td><td style = "text-align: left;">S12029</td><td style = "text-align: left;">20150515</td><td style = "text-align: left;">0-00000000-0</td><td style = "text-align: left;">CS029403000008</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">9</td><td style = "text-align: left;">CS015804000004</td><td style = "text-align: left;">松谷 米蔵</td><td style = "text-align: left;">0</td><td style = "text-align: left;">男性</td><td style = "text-align: left;">1931-05-02</td><td style = "text-align: right;">87</td><td style = "text-align: left;">136-0073</td><td style = "text-align: left;">東京都江東区北砂**********</td><td style = "text-align: left;">S13015</td><td style = "text-align: left;">20150607</td><td style = "text-align: left;">0-00000000-0</td><td style = "text-align: left;">CS015804000004</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">10</td><td style = "text-align: left;">CS033513000180</td><td style = "text-align: left;">安斎 遥</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1962-07-11</td><td style = "text-align: right;">56</td><td style = "text-align: left;">241-0823</td><td style = "text-align: left;">神奈川県横浜市旭区善部町**********</td><td style = "text-align: left;">S14033</td><td style = "text-align: left;">20150728</td><td style = "text-align: left;">6-20080506-5</td><td style = "text-align: left;">CS033513000180</td></tr></tbody></table></div>

# データの分割

---
> J-089: 売上実績がある顧客を、予測モデル構築のため学習用データとテスト用データに分割したい。それぞれ8:2の割合でランダムにデータを分割せよ。

```julia
gd = groupby(df_receipt, :customer_id)
df_sales_customer = combine(gd, :amount => sum => :sum_amount);
df = innerjoin(df_customer, df_sales_customer, on=:customer_id)

using Random
n = nrow(df)
index = shuffle(collect(1:n))
df_customer_train = df[index[1:floor(Int, n*0.8)], :]
df_customer_test = df[index[floor(Int, n*0.8)+1:n], :]
println((nrow(df_customer_train)/n, nrow(df_customer_test)/n))
first(df_customer_train, 5)
```

    (0.7999036840837949, 0.20009631591620516)

<div><div style = "float: left;"><span>5×12 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">customer_name</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">gender</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">postal_cd</th><th style = "text-align: left;">address</th><th style = "text-align: left;">application_store_cd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">status_cd</th><th style = "text-align: left;">sum_amount</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th><th title = "Int64" style = "text-align: left;">Int64</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS027411000001</td><td style = "text-align: left;">長田 遥</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1973-09-14</td><td style = "text-align: right;">45</td><td style = "text-align: left;">251-0861</td><td style = "text-align: left;">神奈川県藤沢市大庭**********</td><td style = "text-align: left;">S14027</td><td style = "text-align: left;">20150720</td><td style = "text-align: left;">A-20100214-A</td><td style = "text-align: right;">2382</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS023615000111</td><td style = "text-align: left;">松井 幸子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1956-06-23</td><td style = "text-align: right;">62</td><td style = "text-align: left;">212-0016</td><td style = "text-align: left;">神奈川県川崎市幸区南幸町**********</td><td style = "text-align: left;">S14023</td><td style = "text-align: left;">20150313</td><td style = "text-align: left;">8-20091213-6</td><td style = "text-align: right;">1504</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS040214000008</td><td style = "text-align: left;">佐々木 由美子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1994-05-30</td><td style = "text-align: right;">24</td><td style = "text-align: left;">194-0001</td><td style = "text-align: left;">東京都町田市つくし野**********</td><td style = "text-align: left;">S14040</td><td style = "text-align: left;">20150811</td><td style = "text-align: left;">F-20100901-F</td><td style = "text-align: right;">13523</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">4</td><td style = "text-align: left;">CS038615000134</td><td style = "text-align: left;">杉山 美智子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td><td style = "text-align: left;">1949-04-03</td><td style = "text-align: right;">69</td><td style = "text-align: left;">279-0043</td><td style = "text-align: left;">千葉県浦安市富士見**********</td><td style = "text-align: left;">S13038</td><td style = "text-align: left;">20150723</td><td style = "text-align: left;">5-20100705-4</td><td style = "text-align: right;">608</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">5</td><td style = "text-align: left;">CS011615000108</td><td style = "text-align: left;">岡本 めぐみ</td><td style = "text-align: left;">9</td><td style = "text-align: left;">不明</td><td style = "text-align: left;">1950-03-06</td><td style = "text-align: right;">69</td><td style = "text-align: left;">223-0058</td><td style = "text-align: left;">神奈川県横浜市港北区新吉田東**********</td><td style = "text-align: left;">S14011</td><td style = "text-align: left;">20141112</td><td style = "text-align: left;">6-20081209-3</td><td style = "text-align: right;">898</td></tr></tbody></table></div>

---
> J-090: レシート明細データ（df_receipt）は2017年1月1日〜2019年10月31日までのデータを有している。売上金額（amount）を月次で集計し、学習用に12ヶ月、テスト用に6ヶ月の時系列モデル構築用データを3セット作成せよ。

```julia
temp = copy(df_receipt)
temp.sales_ymd .÷= 100
gd = groupby(temp, :sales_ymd)
df = combine(gd, :amount => sum => :sum_amount) |>
    @rename(:sales_ymd => :sales_ym) |>
    DataFrame
function sampling()
    start12 = rand(1:23, 1)[1]
    start6  = rand(1:29, 1)[1]
    df_train_1 = df[start12:start12+11, :];
    df_test_1  = df[start6:start6+5, :];
    return df_train_1, df_test_1
end
train, test = sampling();
println(train)
println(test)
```

    [1m12×2 DataFrame[0m
    [1m Row [0m│[1m sales_ym [0m[1m sum_amount [0m
         │[90m Int64    [0m[90m Int64      [0m
    ─────┼──────────────────────
       1 │   201705      884010
       2 │   201706      894242
       3 │   201707      959205
       4 │   201708      954836
       5 │   201709      902037
       6 │   201710      905739
       7 │   201711      932157
       8 │   201712      939654
       9 │   201801      944509
      10 │   201802      864128
      11 │   201803      946588
      12 │   201804      937099
    [1m6×2 DataFrame[0m
    [1m Row [0m│[1m sales_ym [0m[1m sum_amount [0m
         │[90m Int64    [0m[90m Int64      [0m
    ─────┼──────────────────────
       1 │   201702      764413
       2 │   201703      962945
       3 │   201704      847566
       4 │   201705      884010
       5 │   201706      894242
       6 │   201707      959205

---
> J-091: 顧客データ（df_customer）の各顧客に対し、売上実績がある顧客数と売上実績がない顧客数が1:1となるようにアンダーサンプリングで抽出せよ。

```julia
gd = groupby(df_receipt, :customer_id)
df = combine(gd, :amount => sum => :sum_amount)
df2 = leftjoin(df_customer, df, on=:customer_id) |>
    @replacena(0) |>
    @mutate(flag = _.sum_amount > 0) |>
    DataFrame
gd2 = groupby(df2, :flag)
(n1, n2) = (nrow(gd2[1]), nrow(gd2[2]))

using StatsBase
index = sample(1:n1, n2, replace=false)  # 非復元抽出
df_amount_0 = gd2[2];
df_amount_1 = gd2[1][index, :]
nrow(df_amount_0), nrow(df_amount_1)
```

    (8306, 8306)

# 第三正規形，非正規化

---
> J-092: 顧客データ（df_customer）の性別について、第三正規形へと正規化せよ。

```julia
df_customer_std = df_customer |>
    @select(-:gender) |>
    @take(3) |>
    DataFrame
```

<div><div style = "float: left;"><span>3×10 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">customer_id</th><th style = "text-align: left;">customer_name</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">birth_day</th><th style = "text-align: left;">age</th><th style = "text-align: left;">postal_cd</th><th style = "text-align: left;">address</th><th style = "text-align: left;">application_store_cd</th><th style = "text-align: left;">application_date</th><th style = "text-align: left;">status_cd</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String31" style = "text-align: left;">String31</th><th title = "String" style = "text-align: left;">String</th><th title = "Date" style = "text-align: left;">Date</th><th title = "Int64" style = "text-align: left;">Int64</th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th><th title = "String" style = "text-align: left;">String</th><th title = "String15" style = "text-align: left;">String15</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">CS021313000114</td><td style = "text-align: left;">大野 あや子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">1981-04-29</td><td style = "text-align: right;">37</td><td style = "text-align: left;">259-1113</td><td style = "text-align: left;">神奈川県伊勢原市粟窪**********</td><td style = "text-align: left;">S14021</td><td style = "text-align: left;">20150905</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">CS037613000071</td><td style = "text-align: left;">六角 雅彦</td><td style = "text-align: left;">9</td><td style = "text-align: left;">1952-04-01</td><td style = "text-align: right;">66</td><td style = "text-align: left;">136-0076</td><td style = "text-align: left;">東京都江東区南砂**********</td><td style = "text-align: left;">S13037</td><td style = "text-align: left;">20150414</td><td style = "text-align: left;">0-00000000-0</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">CS031415000172</td><td style = "text-align: left;">宇多田 貴美子</td><td style = "text-align: left;">1</td><td style = "text-align: left;">1976-10-04</td><td style = "text-align: right;">42</td><td style = "text-align: left;">151-0053</td><td style = "text-align: left;">東京都渋谷区代々木**********</td><td style = "text-align: left;">S13031</td><td style = "text-align: left;">20150529</td><td style = "text-align: left;">D-20100325-C</td></tr></tbody></table></div>

```julia
df_gender_std = df_customer |>
    @select(:gender_cd, :gender) |>
    @unique() |>
    @take(3) |>
    DataFrame
```

<div><div style = "float: left;"><span>3×2 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">gender_cd</th><th style = "text-align: left;">gender</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String" style = "text-align: left;">String</th><th title = "String7" style = "text-align: left;">String7</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">1</td><td style = "text-align: left;">女性</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">9</td><td style = "text-align: left;">不明</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">0</td><td style = "text-align: left;">男性</td></tr></tbody></table></div>

---
> J-093: 商品データ（df_product）では各カテゴリのコード値だけを保有し、カテゴリ名は保有していない。カテゴリデータ（df_category）と組み合わせて非正規化し、カテゴリ名を保有した新たな商品データを作成せよ。

```julia
df_product_full = innerjoin(df_product, df_category[!, [:category_small_cd, :category_major_name, :category_medium_name, :category_small_name]], on=:category_small_cd);
first(df_product_full, 3)
```

<div><div style = "float: left;"><span>3×9 DataFrame</span></div><div style = "clear: both;"></div></div><div class = "data-frame" style = "overflow-x: scroll;"><table class = "data-frame" style = "margin-bottom: 6px;"><thead><tr class = "header"><th class = "rowNumber" style = "font-weight: bold; text-align: right;">Row</th><th style = "text-align: left;">product_cd</th><th style = "text-align: left;">category_major_cd</th><th style = "text-align: left;">category_medium_cd</th><th style = "text-align: left;">category_small_cd</th><th style = "text-align: left;">unit_price</th><th style = "text-align: left;">unit_cost</th><th style = "text-align: left;">category_major_name</th><th style = "text-align: left;">category_medium_name</th><th style = "text-align: left;">category_small_name</th></tr><tr class = "subheader headerLastRow"><th class = "rowNumber" style = "font-weight: bold; text-align: right;"></th><th title = "String15" style = "text-align: left;">String15</th><th title = "String" style = "text-align: left;">String</th><th title = "String" style = "text-align: left;">String</th><th title = "String" style = "text-align: left;">String</th><th title = "Union{Missing, Int64}" style = "text-align: left;">Int64?</th><th title = "Union{Missing, Int64}" style = "text-align: left;">Int64?</th><th title = "String" style = "text-align: left;">String</th><th title = "String" style = "text-align: left;">String</th><th title = "String" style = "text-align: left;">String</th></tr></thead><tbody><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">1</td><td style = "text-align: left;">P040101001</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">198</td><td style = "text-align: right;">149</td><td style = "text-align: left;">惣菜</td><td style = "text-align: left;">御飯類</td><td style = "text-align: left;">弁当類</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">2</td><td style = "text-align: left;">P040101002</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">218</td><td style = "text-align: right;">164</td><td style = "text-align: left;">惣菜</td><td style = "text-align: left;">御飯類</td><td style = "text-align: left;">弁当類</td></tr><tr><td class = "rowNumber" style = "font-weight: bold; text-align: right;">3</td><td style = "text-align: left;">P040101003</td><td style = "text-align: left;">04</td><td style = "text-align: left;">0401</td><td style = "text-align: left;">040101</td><td style = "text-align: right;">230</td><td style = "text-align: right;">173</td><td style = "text-align: left;">惣菜</td><td style = "text-align: left;">御飯類</td><td style = "text-align: left;">弁当類</td></tr></tbody></table></div>

# ファイル入出力

---
> J-094: 093で作成したカテゴリ名付き商品データを以下の仕様でファイル出力せよ。
>
> |ファイル形式|ヘッダ有無|文字エンコーディング|
> |:--:|:--:|:--:|
> |CSV（カンマ区切り）|有り|UTF-8|
> 
> ファイル出力先のパスは以下のようにすること
> 
> |出力先|
> |:--:|
> |./data|

```julia
CSV.write("-094.csv", df_product_full)
```

    "-094.csv"

**これ以降の問題は重要なものではないのでパスする**

---
> J-095: 093で作成したカテゴリ名付き商品データを以下の仕様でファイル出力せよ。
>
> |ファイル形式|ヘッダ有無|文字エンコーディング|
> |:--:|:--:|:--:|
> |CSV（カンマ区切り）|有り|CP932|
> 
> ファイル出力先のパスは以下のようにすること。
> 
> |出力先|
> |:--:|
> |./data|

---
> J-096: 093で作成したカテゴリ名付き商品データを以下の仕様でファイル出力せよ。
>
> |ファイル形式|ヘッダ有無|文字エンコーディング|
> |:--:|:--:|:--:|
> |CSV（カンマ区切り）|無し|UTF-8|
> 
> ファイル出力先のパスは以下のようにすること。
> 
> |出力先|
> |:--:|
> |./data|

---
> J-097: 094で作成した以下形式のファイルを読み込み、データを3件を表示させて正しく取り込まれていることを確認せよ。
> 
> |ファイル形式|ヘッダ有無|文字エンコーディング|
> |:--:|:--:|:--:|
> |CSV（カンマ区切り）|有り|UTF-8|

---
> J-098: 096で作成した以下形式のファイルを読み込み、データを3件を表示させて正しく取り込まれていることを確認せよ。
> 
> |ファイル形式|ヘッダ有無|文字エンコーディング|
> |:--:|:--:|:--:|
> |CSV（カンマ区切り）|ヘッダ無し|UTF-8|

---
> J-099: 093で作成したカテゴリ名付き商品データを以下の仕様でファイル出力せよ。
>
> |ファイル形式|ヘッダ有無|文字エンコーディング|
> |:--:|:--:|:--:|
> |TSV（タブ区切り）|有り|UTF-8|
> 
> ファイル出力先のパスは以下のようにすること
> 
> |出力先|
> |:--:|
> |./data|

---
> J-100: 099で作成した以下形式のファイルを読み込み、データを3件を表示させて正しく取り込まれていることを確認せよ。
> 
> |ファイル形式|ヘッダ有無|文字エンコーディング|
> |:--:|:--:|:--:|
> |TSV（タブ区切り）|有り|UTF-8|

~~# これで１００本終わりです。おつかれさまでした！