カテゴリ型データ
pandasにおけるカテゴリ型
code: Python
N = len(fruits)
df = pd.DataFrame({'fruit': fruits,
'basket_id': np.arange(N),
'count': np.random.randint(3, 15, size=N),
'weight': np.random.uniform(0, 4, size=N)},
df
--------------------------------------------------------------------------
basket_id fruit count weight
0 0 apple 5 3.858058
1 1 orange 8 2.612708
2 2 apple 4 2.995627
3 3 apple 7 2.614279
4 4 apple 12 2.990859
5 5 orange 8 3.845227
6 6 apple 5 0.033553
7 7 apple 4 0.425778
--------------------------------------------------------------------------
code: Python
# カテゴリ型へ変換
fruit_cat = df'fruit'.astype('category') fruit_cat
--------------------------------------------------------------------------
0 apple
1 orange
2 apple
3 apple
4 apple
5 orange
6 apple
7 apple
Name: fruit, dtype: category
--------------------------------------------------------------------------
code: Python
c = fruit_cat.values
type(c)
--------------------------------------------------------------------------
pandas.core.categorical.Categorical
--------------------------------------------------------------------------
code: Python
c.categories
--------------------------------------------------------------------------
--------------------------------------------------------------------------
code: Python
c.codes
--------------------------------------------------------------------------
--------------------------------------------------------------------------
code: Python
my_categories
--------------------------------------------------------------------------
--------------------------------------------------------------------------
code: Python
my_cats_2 = pd.Categorical.from_codes(codes, categories)
my_cats_2
--------------------------------------------------------------------------
--------------------------------------------------------------------------
code: Python
ordered_cat = pd.Categorical.from_codes(codes, categories, ordered=True)
ordered_cat
--------------------------------------------------------------------------
--------------------------------------------------------------------------
code: Python
my_cats_2.as_ordered()
--------------------------------------------------------------------------
--------------------------------------------------------------------------
カテゴリを用いた計算
code: Python
np.random.seed(12345)
draws = np.random.randn(1000)
--------------------------------------------------------------------------
--------------------------------------------------------------------------
code: Python
bins = pd.qcut(draws, 4)
bins
--------------------------------------------------------------------------
(-0.684, -0.0101, (-0.0101, 0.63], (-0.684, -0.0101], (-0.684, -0.0101], (0.63, 3.928], ..., (-0.0101, 0.63], (-0.684, -0.0101], (-2.9499999999999997, -0.684], (-0.0101, 0.63], (0.63, 3.928]] Length: 1000
--------------------------------------------------------------------------
code: Python
bins
--------------------------------------------------------------------------
Q2, Q3, Q2, Q2, Q4, ..., Q3, Q2, Q1, Q3, Q4 Length: 1000
--------------------------------------------------------------------------
code: Python
--------------------------------------------------------------------------
array(1, 2, 1, 1, 3, 3, 2, 2, 3, 3, dtype=int8) --------------------------------------------------------------------------
code: Python
bins = pd.Series(bins, name='quartile')
results = (pd.Series(draws)
.groupby(bins)
.reset_index())
results
--------------------------------------------------------------------------
quartile count min max
0 Q1 250 -2.949343 -0.685484
1 Q2 250 -0.683066 -0.010115
2 Q3 250 -0.010032 0.628894
3 Q4 250 0.634238 3.927528
--------------------------------------------------------------------------
code: Python
--------------------------------------------------------------------------
0 Q1
1 Q2
2 Q3
3 Q4
Name: quartile, dtype: category
--------------------------------------------------------------------------
カテゴリメソッド
code: Python
cat_s = s.astype('category')
cat_s
--------------------------------------------------------------------------
0 a
1 b
2 c
3 d
4 a
5 b
6 c
7 d
dtype: category
--------------------------------------------------------------------------
code: Python
cat_s.cat.codes
--------------------------------------------------------------------------
0 0
1 1
2 2
3 3
4 0
5 1
6 2
7 3
dtype: int8
--------------------------------------------------------------------------
code: Python
cat_s.cat.categories
--------------------------------------------------------------------------
--------------------------------------------------------------------------
code: Python
cat_s2 = cat_s.cat.set_categories(actual_categories)
cat_s2
--------------------------------------------------------------------------
0 a
1 b
2 c
3 d
4 a
5 b
6 c
7 d
dtype: category
--------------------------------------------------------------------------
code: Python
cat_s.value_counts()
--------------------------------------------------------------------------
d 2
c 2
b 2
a 2
dtype: int64
--------------------------------------------------------------------------
code: Python
cat_s2.value_counts()
--------------------------------------------------------------------------
d 2
c 2
b 2
a 2
e 0
dtype: int64
--------------------------------------------------------------------------
code: Python
cat_s3
--------------------------------------------------------------------------
0 a
1 b
4 a
5 b
dtype: category
--------------------------------------------------------------------------
code: Python
cat_s3.cat.remove_unused_categories()
--------------------------------------------------------------------------
0 a
1 b
4 a
5 b
dtype: category
Categories (2, object): a, b --------------------------------------------------------------------------
モデリング用のダミー変数の作成
code: Python
cat_s
--------------------------------------------------------------------------
0 a
1 b
2 c
3 d
4 a
5 b
6 c
7 d
dtype: category
--------------------------------------------------------------------------
code: Python
pd.get_dummies(cat_s)
--------------------------------------------------------------------------
a b c d
0 1 0 0 0
1 0 1 0 0
2 0 0 1 0
3 0 0 0 1
4 1 0 0 0
5 0 1 0 0
6 0 0 1 0
7 0 0 0 1
--------------------------------------------------------------------------