2013-07-02 32 views
6

我对Python/Pandas相对较新,并且很努力从pd.Dataframe中提取正确的数据。我实际上有与3列的数据帧:从熊猫Dataframe中提取在特定列中具有特定值的所有行

data = 

Position Letter Value 
1  a  TRUE 
2  f  FALSE 
3  c  TRUE 
4  d  TRUE 
5  k  FALSE 

我想要做的就是把所有真行到一个新的数据帧,这样的答案是什么:

answer = 

Position Letter Value 
1  a  TRUE 
3  c  TRUE 
4  d  TRUE 

我知道您可以访问特定列使用

data['Value'] 

但我如何提取所有TRUE行?

感谢您的帮助和建议,

亚历

+0

可能重复[?如何通过“内”,“在” /筛选大熊猫的数据帧行(http://stackoverflow.com/问题/ 12065885/how-to-filter-the-dataframe-rows-of-pandas-by-in-in) – DontDivideByZero

回答

13

您可以测试值为true:

In [11]: data['Value'] == True 
Out[11]: 
0  True 
1 False 
2  True 
3  True 
4 False 
Name: Value, dtype: bool 

,然后用花哨的索引拉出那些行:

In [12]: data[data['Value'] == True] 
Out[12]: 
    Position Letter Value 
0   1  a True 
2   3  c True 
3   4  d True 

*注意:如果这些值实际上是字符串'TRUE''FALSE'(!他们可能不应该),然后使用:

data['Value'] == 'TRUE' 
+0

以及何时使用:data ['Value'] =='TRUE''? –

+1

它不适用于data ['Value'] =='TRUE',但可以与data [data ['Value'] == True] – user1083734

相关问题