The Python Oracle

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

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: City Beneath the Waves Looping

--

Chapters
00:00 Extracting All Rows From Pandas Dataframe That Have Certain Value In A Specific Column
00:43 Accepted Answer Score 23
01:13 Answer 2 Score 0
01:30 Thank you

--

Full question
https://stackoverflow.com/questions/1742...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas #dataframe

#avk47



ACCEPTED ANSWER

Score 23


You can test which Values are True:

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 out those rows:

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 strings 'TRUE' and 'FALSE' (they probably shouldn't be!) then use:

data['Value'] == 'TRUE'



ANSWER 2

Score 0


You can wrap your value/values in a list and do the following:

new_df = df.loc[df['yourColumnName'].isin(['your', 'list', 'items'])]

This will return a new dataframe consisting of rows where your list items match your column name in df.