カテゴリ型データ
pandasにおけるカテゴリ型
code: Python
fruits = 'apple', 'orange', 'apple', 'apple' * 2
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)},
columns='basket_id', 'fruit', 'count', 'weight')
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
Categories (2, object): apple, orange
--------------------------------------------------------------------------
code: Python
c = fruit_cat.values
type(c)
--------------------------------------------------------------------------
pandas.core.categorical.Categorical
--------------------------------------------------------------------------
code: Python
c.categories
--------------------------------------------------------------------------
Index('apple', 'orange', dtype='object')
--------------------------------------------------------------------------
code: Python
c.codes
--------------------------------------------------------------------------
array(0, 1, 0, 0, 0, 1, 0, 0, dtype=int8)
--------------------------------------------------------------------------
code: Python
my_categories = pd.Categorical('foo', 'bar', 'baz', 'foo', 'bar')
my_categories
--------------------------------------------------------------------------
foo, bar, baz, foo, bar
Categories (3, object): bar, baz, foo
--------------------------------------------------------------------------
code: Python
categories = 'foo', 'bar', 'baz'
codes = 0, 1, 2, 0, 0, 1
my_cats_2 = pd.Categorical.from_codes(codes, categories)
my_cats_2
--------------------------------------------------------------------------
foo, bar, baz, foo, foo, bar
Categories (3, object): foo, bar, baz
--------------------------------------------------------------------------
code: Python
ordered_cat = pd.Categorical.from_codes(codes, categories, ordered=True)
ordered_cat
--------------------------------------------------------------------------
foo, bar, baz, foo, foo, bar
Categories (3, object): foo < bar < baz
--------------------------------------------------------------------------
code: Python
my_cats_2.as_ordered()
--------------------------------------------------------------------------
foo, bar, baz, foo, foo, bar
Categories (3, object): foo < bar < baz
--------------------------------------------------------------------------
カテゴリを用いた計算
code: Python
np.random.seed(12345)
draws = np.random.randn(1000)
draws:5
--------------------------------------------------------------------------
array(-0.2047, 0.4789, -0.5194, -0.5557, 1.9658)
--------------------------------------------------------------------------
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
Categories (4, intervalfloat64): (-2.9499999999999997, -0.684 < (-0.684, -0.0101] < (-0.0101, 0.63] < (0.63, 3.928]]
--------------------------------------------------------------------------
code: Python
bins = pd.qcut(draws, 4, labels='Q1', 'Q2', 'Q3', 'Q4')
bins
--------------------------------------------------------------------------
Q2, Q3, Q2, Q2, Q4, ..., Q3, Q2, Q1, Q3, Q4
Length: 1000
Categories (4, object): Q1 < Q2 < Q3 < Q4
--------------------------------------------------------------------------
code: Python
bins.codes:10
--------------------------------------------------------------------------
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)
.agg('count', 'min', 'max')
.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
results'quartile'
--------------------------------------------------------------------------
0 Q1
1 Q2
2 Q3
3 Q4
Name: quartile, dtype: category
Categories (4, object): Q1 < Q2 < Q3 < Q4
--------------------------------------------------------------------------
カテゴリメソッド
code: Python
s = pd.Series('a', 'b', 'c', 'd' * 2)
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
Categories (4, object): a, b, c, d
--------------------------------------------------------------------------
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
--------------------------------------------------------------------------
Index('a', 'b', 'c', 'd', dtype='object')
--------------------------------------------------------------------------
code: Python
actual_categories = 'a', 'b', 'c', 'd', 'e'
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
Categories (5, object): a, b, c, d, e
--------------------------------------------------------------------------
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 = cat_s[cat_s.isin('a', 'b')]
cat_s3
--------------------------------------------------------------------------
0 a
1 b
4 a
5 b
dtype: category
Categories (4, object): a, b, c, d
--------------------------------------------------------------------------
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 = pd.Series('a', 'b', 'c', 'd' * 2, dtype='category')
cat_s
--------------------------------------------------------------------------
0 a
1 b
2 c
3 d
4 a
5 b
6 c
7 d
dtype: category
Categories (4, object): a, b, c, d
--------------------------------------------------------------------------
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
--------------------------------------------------------------------------