crosstab()の使用例

```
ykishi@dezembro crosstab-test % cat mytest1.py; echo; ./mytest1.py
```
```Python
#!/usr/bin/env python3

import numpy as np
import pandas as pd

size = 30
sex = np.random.choice(["male", "female"], size=size)
item = np.random.choice(["Jazz", "R&B", "Classic"], size=size)
price = np.random.choice([100, 1000, 10000], size=size)
df = pd.DataFrame({"sex": sex, "item": item, "price": price})

print(df)
print()

print(
    """
# ============================================
# item, sexでの発生回数
# ============================================
"""
)
print(pd.crosstab(index=df["item"], columns=df["sex"]))
print()

print(
    """
# ============================================
# 行をitem, priceを指定した組み合わせで発生回数を求める
# ============================================
"""
)
print(pd.crosstab(index=[df["item"], df["price"]], columns=df["sex"]))
print()

print(
    """
# ============================================
# 列をsex, princeに指定して発生回数を求める
# ============================================
"""
)
print(pd.crosstab(index=df["item"], columns=[df["sex"], df["price"]]))
print()

print(
    """
# ============================================
# 発生回数でなく、合計あるいは平均を求める
# ============================================
"""
)

print(
    pd.crosstab(index=df["item"], columns=df["sex"], values=df["price"], aggfunc="sum")
)
print()

print(
    pd.crosstab(index=df["item"], columns=df["sex"], values=df["price"], aggfunc="mean")
)
print()

print(
    """
# ============================================
# 小計を求める
# ============================================
"""
)
print(pd.crosstab(index=df["item"], columns=df["sex"], margins=True))
print()

print(pd.crosstab(index=df["item"], columns=df["sex"], margins=True, margins_name="小計"))
print()

print(
    """
# ============================================
# 出力結果を正規化する
# ============================================
"""
)
print(pd.crosstab(index=df["item"], columns=df["sex"], margins=True))
print()

print(pd.crosstab(index=df["item"], columns=df["sex"], margins=True, normalize=True))
print()

print(pd.crosstab(index=df["item"], columns=df["sex"], margins=True, normalize="index"))
print()

print(
    pd.crosstab(index=df["item"], columns=df["sex"], margins=True, normalize="columns")
)
print()
```
```
       sex     item  price
0     male      R&B   1000
1     male  Classic   1000
2   female     Jazz  10000
3   female      R&B   1000
4     male     Jazz   1000
5   female     Jazz  10000
6   female      R&B  10000
7   female      R&B  10000
8   female      R&B   1000
9   female  Classic  10000
10  female     Jazz   1000
11  female  Classic  10000
12    male  Classic    100
13    male      R&B    100
14  female      R&B   1000
15    male     Jazz   1000
16  female  Classic  10000
17    male     Jazz   1000
18  female     Jazz  10000
19  female      R&B  10000
20    male      R&B   1000
21    male      R&B   1000
22  female      R&B  10000
23  female      R&B    100
24  female  Classic   1000
25  female  Classic   1000
26    male  Classic  10000
27  female     Jazz   1000
28    male  Classic  10000
29  female  Classic   1000


# ============================================
# item, sexでの発生回数
# ============================================

sex      female  male
item                 
Classic       6     4
Jazz          5     3
R&B           8     4


# ============================================
# 行をitem, priceを指定した組み合わせで発生回数を求める
# ============================================

sex            female  male
item    price              
Classic 100         0     1
        1000        3     1
        10000       3     2
Jazz    1000        2     3
        10000       3     0
R&B     100         1     1
        1000        3     3
        10000       4     0


# ============================================
# 列をsex, princeに指定して発生回数を求める
# ============================================

sex     female              male            
price    100   1000  10000 100   1000  10000
item                                        
Classic      0     3     3     1     1     2
Jazz         0     2     3     0     3     0
R&B          1     3     4     1     3     0


# ============================================
# 発生回数でなく、合計あるいは平均を求める
# ============================================

sex      female   male
item                  
Classic   33000  21100
Jazz      32000   3000
R&B       43100   3100

sex      female    male
item                   
Classic  5500.0  5275.0
Jazz     6400.0  1000.0
R&B      5387.5   775.0


# ============================================
# 小計を求める
# ============================================

sex      female  male  All
item                      
Classic       6     4   10
Jazz          5     3    8
R&B           8     4   12
All          19    11   30

sex      female  male  小計
item                     
Classic       6     4  10
Jazz          5     3   8
R&B           8     4  12
小計           19    11  30


# ============================================
# 出力結果を正規化する
# ============================================

sex      female  male  All
item                      
Classic       6     4   10
Jazz          5     3    8
R&B           8     4   12
All          19    11   30

sex        female      male       All
item                                 
Classic  0.200000  0.133333  0.333333
Jazz     0.166667  0.100000  0.266667
R&B      0.266667  0.133333  0.400000
All      0.633333  0.366667  1.000000

sex        female      male
item                       
Classic  0.600000  0.400000
Jazz     0.625000  0.375000
R&B      0.666667  0.333333
All      0.633333  0.366667

sex        female      male       All
item                                 
Classic  0.315789  0.363636  0.333333
Jazz     0.263158  0.272727  0.266667
R&B      0.421053  0.363636  0.400000

ykishi@dezembro crosstab-test % 
```