2017-09-16 3 views

답변

0

일부 범주 형 변수에서 데이터 프레임을 부분 집합하는 경우 정렬 된 인덱스는 loc과 함께 유용합니다.

df = df.sort_index() 
df.loc['b':'c'] 

올바르게 선택 행주는 예를 들어, 다음의 데이터 프레임

import pandas as pd 
index = ['a', 'b', 'c', 'a', 'b', 'c'] 
x = np.arange(6) 
df = pd.DataFrame({"x": x}, 
        index = pd.Categorical(index, categories=['a', 'b', 'c'], ordered=True)) 

df.loc['b':'c']는 오류 준다.

x 
b 1 
b 4 
c 2 
c 5