2017-07-15 82 views
0

我有一個數據幀過濾的價值觀和我保存這個數據幀的第一行如下如何在蟒蛇系列

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: '>' not supported between instances of 'str' and 'int'.

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

+0

您可以通過點擊下面的問題標籤 –

+0

你的數據應該是整數的「編輯」按鈕,對帖子進行編輯,這是字符串。你不能比較整數和字符串。 –

+0

您的意思是「類」將字符串值存儲爲字符串嗎?如果是這樣的話,爲什麼'first_row!= 0'有效? – Nanan

回答

0

使用數據框的查詢功能將返回所需的結果

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