简体   繁体   English

选择熊猫数据框的子集

[英]Selecting a subset of a Pandas DataFrame

I have the following DateFrame: 我有以下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: 我想显示从年到wgt等于27的列。我已经在两行上完成了它:

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

Is there any way to reduce it to one line? 有什么办法可以减少到一行吗?

Is there any way to reduce it to one line? 有什么办法可以减少到一行吗?

You can easily combine the 2 lines into 1: 您可以轻松地将2行合并为1:

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

You can use ix : 您可以使用ix

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

Sample (value 2001 was added): 样本(添加了值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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM