Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - find specific value in entire dataframe

I have a dataframe and I want to search all columns for values that is text 'Apple'. I know how to do it with one column, but how can I apply this to ALL columns? I want to make it a function, so that next time I can directly use it to search for other values in other dateframes.

Thanks.

like image 413
Chloe Avatar asked Dec 03 '18 04:12

Chloe


People also ask

How to get the cell value from the pandas Dataframe?

In this article, we will discuss how to get the cell value from the pandas dataframe. This method is used to get the particular cell data with index function by using the column name dataframe [‘column_name’].loc [dataframe.index [row_number]] Example: Python program to get particular cell using loc () function

How to search for a string in all columns of pandas?

To search for a string in all columns of a Pandas DataFrame we can use two different ways: Let's check two examples on how to use the above techniques in practice. The lambda will iterate over all rows. Then we will convert the values to string - in order to avoid errors.

How to check every column in a Dataframe using Python?

To check every column, you could use for col in df to iterate through the column names, and then call str.contains on each column: Alternatively, you could pass regex=False to str.contains to make the test use the Python in operator; but (in general) using regex is faster. Show activity on this post. Show activity on this post.

How do I print a specific element of a pandas Dataframe?

In this example, I’ll show how to print a specific element of a pandas DataFrame using the row index and the column name. To achieve this, we can use the .at attribute: The data cell at the row index 1 in the variable x2 contains the character “b”.


1 Answers

you can try searching entire dataframe using the below code

df[df.eq("Apple").any(1)]

Using numpy comparison

df[(df.values.ravel() == "Apple").reshape(df.shape).any(1)]

Both are faster smaller records but not sure about large dataset.

like image 181
Vijay Anand Pandian Avatar answered Oct 16 '22 16:10

Vijay Anand Pandian