> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Search for String in the Whole DataFrame in Pandas
- URL: https://datascientyst.com/search-for-string-whole-dataframe-pandas/
- Published: 2021-11-04T09:37:36.000Z
- Updated: 2021-11-04T09:37:36.000Z
- Author: John D K
- Tags: Search

To search for a string in all columns of a Pandas DataFrame we can use two different ways:

**(1) Lambda and str.contains**

```python
df.apply(lambda row: row.astype(str).str.contains('data').any(), axis=1)

```

**(2) np.column\_stack + str.contains**

```python
import numpy as np
mask = np.column_stack([df[col].astype(str).str.contains("data", na=False) for col in df])
df.loc[mask.any(axis=1)]

```

Let's check two examples on how to use the above techniques in practice.

To start with DataFrame like:

```python
from IPython.display import HTML
import pandas as pd

df = pd.DataFrame({
    'id':[1,2,3,4],
    'name':['Softhints\nLinux', 'dataplotplus', 'DataScientyst\nPandas', 'test'],
    'url':['https://www.softhints.com', 'https://dataplotplus.com/', 'https://datascientyst.com', 'test\data']
})

```

which has this data:

| id | name                   | url                       |
| -- | ---------------------- | ------------------------- |
| 1  | Softhints\\nLinux      | https://www.softhints.com |
| 2  | dataplotplus           | https://dataplotplus.com/ |
| 3  | DataScientyst\\nPandas | https://datascientyst.com |
| 4  | test                   | test\\data                |

## Search whole DataFrame with lambda and str.contains

Searching with lambda and str.contains is straightforward:

```python
df.apply(lambda row: row.astype(str).str.contains('data').any(), axis=1)

```

The lambda will iterate over all rows. Then we will convert the values to string - in order to avoid errors.

The converted data will be searched for a string pattern - in this case `data`.

Without method any we will get the search result for each value:

| id    | name  | url   |
| ----- | ----- | ----- |
| False | False | False |
| False | True  | True  |
| False | False | True  |
| False | False | True  |

So method `any` will return True if there is at least one True value per row.

So the final output is:

0 False  
1 True  
2 True  
3 True  
dtype: bool

![](https://datascientyst.com/content/images/2021/11/search-for-string-whole-dataframe-pandas.png)

## Search whole DataFrame with numpy and str.contains

As an alternative solution you can use the Numpy method - `column_stack` to find all values in all columns. This solution is faster than the previous one.

```python
import numpy as np
mask = np.column_stack([df[col].astype(str).str.contains("data", na=False) for col in df])
df.loc[mask.any(axis=1)]

```

**How does it work?**

So the code:

```python
[df[col].astype(str).str.contains("data", na=False) for col in df]

```

will iterate over all columns and then will convert values to string. Then we perform a search for a given value.

The result would be:

```
[0    False
 1    False
 2    False
 3    False
 Name: id, dtype: bool,
 0    False
 1     True
 2    False
 3    False
 Name: name, dtype: bool,
 0    False
 1     True
 2     True
 3     True
 Name: url, dtype: bool]

```

Method `np.column_stack` will convert the above result into `array`:

```
array([[False, False, False],
       [False,  True,  True],
       [False, False,  True],
       [False, False,  True]])

```

Again we are going to use method `any` in order to return a single True or False value per row. The method `loc` will return only the rows which contain the searched value.

```python
df.loc[mask.any(axis=1)]

```

## Resources

- [Notebook](https://github.com/softhints/Pandas-Tutorials/blob/master/find/search-for-string-whole-dataframe-pandas.ipynb?ref=datascientyst.com)
- [pandas.DataFrame.apply](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html?highlight=apply&ref=datascientyst.com#pandas.DataFrame.apply)
- [numpy.column\_stack](https://numpy.org/doc/stable/reference/generated/numpy.column%5Fstack.html?ref=datascientyst.com)
- [pandas.DataFrame.any](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.any.html?highlight=any&ref=datascientyst.com#pandas.DataFrame.any)
- [pandas.Series.any](https://pandas.pydata.org/docs/reference/api/pandas.Series.any.html?highlight=any&ref=datascientyst.com#pandas.Series.any)