1

I have the following DateFrame:

 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
    ...          ...      ...  ...  ...    ...    ...   ... ...  

I want to display columns from year to wgt with values equal 27. I have done it on two lines:

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

Is there any way to reduce it to one line?

2 Answers 2

2

You can use ix:

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

Sample (value 2001 was added):

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
Sign up to request clarification or add additional context in comments.

2 Comments

It was very helpful. What about if I want to display years: 2001 and 2002 and the same time. I've tried df.ix[df.year == 2001 & df.year == 2001, 'year':], but it's not the right solution
You are very close, only add brackets df.ix[(df.year == 2001) & (df.year == 2001), 'year':],
2

Is there any way to reduce it to one line?

You can easily combine the 2 lines into 1:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.