본문 바로가기
Python

python 판다스(pandas) DataFrame categorical 범주형 데이터 작업

by 코딩하는 미토콘드리아 bioinformatics 2023. 11. 18.
반응형

DataFrame categorical 범주형 데이터 작업

 

원래 데이터 유형으로 다시 변환

s = Series(['a','b','a','c','b','d','a'],
 			dtype='category')
s = s.astype('string')


데이터 정렬

s = Series(list('abc'), dtype='category')
print (s.cat.ordered)
s=s.cat.reorder_categories(['b','c','a'])
s = s.sort()
s.cat.ordered = False


카테고리 이름 바꾸기

s = Series(list('abc'), dtype='category')
s.cat.categories = [1, 2, 3] # in place
s = s.cat.rename_categories([4,5,6])

s.cat.categories = ['Group ' + str(i)
 for i in s.cat.categories]


새 카테고리 추가

s = s.cat.add_categories([4])


카테고리 삭제

s = s.cat.remove_categories([4])
s.cat.remove_unused_categories() #inplace

 

 

참고 : https://www.geeksforgeeks.org/pandas-cheat-sheet

 

Pandas Cheat Sheet for Data Science in Python

This cheat sheet provides a quick reference to the most common Pandas commands, covering everything from data loading and manipulation to plotting and visualization. Whether you're a beginner or a seasoned data scientist, this cheat sheet is a valuable res

www.geeksforgeeks.org

 

 

반응형