如何在python系列上过滤值

时间:2017-07-15 15:44:42

标签: python pandas series

我有一个数据框,我保存了这个数据框的第一行,如下所示

first_row = data_model.loc[0,:]

如果我打印出first_row,它看起来像是

label                class
wa                     3
not                    0
im                     2
time                   0
see                    2
like                   0
going                  2
amp                    1
get                    1
one                    0

我只想保持大于0的值,所以我使用first_row[first_row > 0],但是收到如下错误消息:

  

TypeError:'>' ' str'的实例之间不支持和' int'。

如果我使用first_row[first_row != 0],我会得到我想要的。

1 个答案:

答案 0 :(得分:0)

使用DataFrame的查询功能将返回所需的结果

df=pd.DataFrame({"label:["wa","not","im","time","see","lke","going"],"amp":`[3,0,2,0,2,0,2]})

df=df.query("amp>0")

print(df)

           amp  label
        0    3     wa
        2    2     im
        4    2    see
        6    2  going