2016-08-01 12 views
1

의 하위 집합을 선택 I 다음 DateFrame 있습니다팬더 DataFrame

record_id month day year plot species sex wgt 
    33320  33321  1 12 2002  1  DM M 44 
    33321  33322  1 12 2002  1  DO M 58 
    ...   ...  ... ... ... ... ... ... ... 

내가 값이 동일과 WGT에 대비 열을 표시하려면을 (27) 나는 두 줄에했을 :

df_slice = df[df.year == 27] 
df_slice = df_slice.ix[:,'year':] 

한 줄로 줄이는 방법이 있습니까?

답변

2

당신은 ix 사용할 수 있습니다

print (df.ix[df.year == 27, 'year':]) 

샘플 (값 2001 첨가) :

print (df) 
    record  id month day year plot species sex wgt 
0 33320 33321  1 12 2001  1  DM M 44 
1 33321 33322  1 12 2002  1  DO M 58 

print (df.ix[df.year == 2001, 'year':]) 
    year plot species sex wgt 
0 2001  1  DM M 44 
+0

그것을 매우 도움이되었습니다. 내가 년 : 2001 년과 2002 년과 같은 시간을 표시하려면 어떨까요? 나는 df.ix를 시도했다. (df.year == 2001 & df.year == 2001, 'year':], 그러나 그것은 올바른 해결책이 아니다. – Monica

+0

당신은 매우 가깝다. 오직 괄호'df.ix [(df .year == 2001) & (df.year == 2001), 'year':], ' – jezrael

+1

또한 내 대답이 도움이된다면 [accept] (http://meta.stackexchange.com/questions/5234/)를 잊어 버리지 마십시오. 답변을받는 것). – jezrael

2

한 줄로 줄이는 방법이 있습니까?

쉽게 1로 2 개 라인을 결합 할 수 있습니다 :

df_slice = df[df.year == 27].ix[:,'year':]