Ch12~13. 고급 pandas & 파이썬 모델링 라이브러리
values = pd.Series([0,1,0,0]*2)
values
>>
0 0
1 1
2 0
3 0
4 0
5 1
6 0
7 0
dtype: int64dim=pd.Series(['apple','orange'])
dim
>>
0 apple
1 orange
dtype: objectdim.take(values)
>>
0 apple
1 orange
0 apple
0 apple
0 apple
1 orange
0 apple
0 apple
dtype: object
-- take 메서드를 사용하면 Series내에 저장된 원래 문자열을 구할 수 있다pandas의 Categorical
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 13 2.270653
1 1 orange 12 2.199684
2 2 apple 7 1.633151
3 3 apple 11 3.259698
4 4 apple 13 2.032409
5 5 orange 10 0.481295
6 6 apple 7 2.822669
7 7 apple 6 1.411839fruit_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]
-- fruit_cat의 값은 NumPy 배열이 아니라
-- pandas.Categorical의 인스턴스다Last updated