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.
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
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.
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.
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”.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With