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

从具有特定值的DF中提取行:python,python,dataframe,Python,Dataframe,我希望能够从下面的这些值中提取特定列。例如,如果我想要对数质量大于7.35但小于7.46的柱(物种),我该如何编码。(这是一张照片) 您可以像这样分割数据帧: df[(df['log'] > 7.35 ) & (df['log'] < 7.46)]['species'] df[(df['log']>7.35)和(df['log']7.35)和(df['log:']7.35)和(df['log:']

我希望能够从下面的这些值中提取特定列。例如,如果我想要对数质量大于7.35但小于7.46的柱(物种),我该如何编码。(这是一张照片)


您可以像这样分割数据帧:

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

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

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
[15]中的
:df.head()
出[15]:
ID:种类:对数:质量:参考:
0 4676神秘埃图8.00 100000000.0 68
14683菲索勒斯7.857000000.068
24720 marginata 7.5132000000.068
34684 novaeangliae 7.48 30000000.0 68
44717罗布斯塔斯7.4528500000.068
在[16]中:df[(df['log:']>7.35)和(df['log:']<7.46)]
出[16]:
ID:种类:对数:质量:参考:
44717罗布斯塔斯7.4528500000.068
54678冰川7.3623000000.068
64677澳大利亚7.3623000000.068
在[18]:df[(df['log:']>7.35)和(df['log:']<7.46)]['species:']
出[18]:
4罗布斯托斯
5冰川
6澳大利亚
名称:种类:,数据类型:对象

请参见您可以将.loc与pandas series.between一起使用,如下所示

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

4     robustus
5    glacialis
6    australis
4     robustus
5    glacialis
6    australis