Extract all rows from pandas Dataframe that have a specific value in a specific column

I'm relatively new to Python / Pandas and struggling with extracting the correct data from pd.Dataframe. In fact, I have a Dataframe with 3 columns:

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

What I want to do is put all the TRUE rows in a new Dataframe so that the answer is as follows:

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

I know that you can access a specific column using

 data['Value'] 

but how can I extract all TRUE strings?

Thanks for any help and advice,

Alex

+6
source share
1 answer

You can check which values ​​are correct:

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

and then use fancy indexing to pull these lines:

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

* Note: if the values ​​are actually the lines 'TRUE' and 'FALSE' (they probably shouldn't be!), Then use:

 data['Value'] == 'TRUE' 
+13
source

Source: https://habr.com/ru/post/948533/


All Articles