你能解释一下这是怎么回事吗?

时间:2017-07-04 12:52:49

标签: python pandas

df = df.loc[-df['Retention time (min)'] < 0]

这是否意味着它正在删除保留时间小于0的行。

1 个答案:

答案 0 :(得分:3)

它被称为boolean indexing

只需按条件过滤数据行,此处:

-df['Retention time (min)'] < 0

loc没有必要:

df = df[-df['Retention time (min)'] < 0]

但是如果需要过滤器,还需要一些列loc

df = df.loc[-df['Retention time (min)'] < 0, 'col1']

样品:

df = pd.DataFrame({'Retention time (min)':[-1,2,3,-6,4,5],
                   'b':[4,5,4,5,5,4]})

print (df)
   Retention time (min)  b
0                    -1  4
1                     2  5
2                     3  4
3                    -6  5
4                     4  5
5                     5  4

df1 = df[-df['Retention time (min)'] < 0]
print (df1)
   Retention time (min)  b
1                     2  5
2                     3  4
4                     4  5
5                     5  4

df2 = df.loc[-df['Retention time (min)'] < 0, 'b']
print (df2)
1    5
2    4
4    5
5    4
Name: b, dtype: int64

与以下内容相同:

df1 = df[df['Retention time (min)'] > 0]
print (df1)
   Retention time (min)  b
1                     2  5
2                     3  4
4                     4  5
5                     5  4

df2 = df.loc[df['Retention time (min)'] > 0, 'b']
print (df2)
1    5
2    4
4    5
5    4
Name: b, dtype: int64