从DF中提取具有特定值的行:python

时间:2017-03-10 20:01:57

标签: python dataframe

我希望能够从下面的这些值中提取特定列。因此,例如,如果我想要具有> 1的对数质量的列(种类) 7.35但是<然后7.46,我将如何编码。 (这是一只熊猫DF)

     ID:       species:      log:     mass:                     ref:
    4676     mysticetus      8.00  100000000.0                   68  
    4683       physalus      7.85   70000000.0                   68  
    4720      marginata      7.51   32000000.0                   68  
    4684   novaeangliae      7.48   30000000.0                   68  
    4717       robustus      7.45   28500000.0                   68  
    4678      glacialis      7.36   23000000.0                   68  
    4677      australis      7.36   23000000.0                   68  

3 个答案:

答案 0 :(得分:0)

您可以像这样对数据帧进行切片:

df[(df['log'] > 7.35 ) & (df['log'] < 7.46)]['species']

答案 1 :(得分:0)

面具可以让你有效地做这类事情:

In [15]: df.head()
Out[15]: 
    ID:      species:  log:        mass:  ref:
0  4676    mysticetus  8.00  100000000.0    68
1  4683      physalus  7.85   70000000.0    68
2  4720     marginata  7.51   32000000.0    68
3  4684  novaeangliae  7.48   30000000.0    68
4  4717      robustus  7.45   28500000.0    68

In [16]: df[(df['log:'] > 7.35) & (df['log:'] < 7.46)]
Out[16]: 
    ID:   species:  log:       mass:  ref:
4  4717   robustus  7.45  28500000.0    68
5  4678  glacialis  7.36  23000000.0    68
6  4677  australis  7.36  23000000.0    68

In [18]: df[(df['log:'] > 7.35) & (df['log:'] < 7.46)]['species:']
Out[18]: 
4     robustus
5    glacialis
6    australis
Name: species:, dtype: object

请参阅http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing

答案 2 :(得分:0)

您可以像这样使用.loc和pandas series.between

df.loc[df['log:'].between(7.35, 7.46), 'species:']

给你

4     robustus
5    glacialis
6    australis